2016-02-23 124 views
0

我试图根据基本模板创建一个新页面模板,自定义页面基于另一个(父)模板创建Wordpress模板

我想什么我自定义页面模板做的是创造一些HTML,定义在那里将其添加到基本模板,然后只包括基本模板。

想要做的就是复制和模板的内容粘贴到我自定义页面模板,那么就修改它。为什么?因为它有助于维护 - 我希望能够在将来修改基本模板,并且任何更改都会自动应用于我的模板。

这里是什么,我已经在我的自定义页面模板到目前为止得到了一个例子:

<?php 
    /* Template Name: Custom Page */ 
    // this template is based on the page template, with some additional content. 
    // We'll use ob_start to get the content, and assign it to the_content, by wrapping it in a function, then include the page template 
    function customPageExtraContent($content){ 
     ob_start(); 
?> 
     <!-- Custom content goes here --> 
     <div> 
      <form> 
       <label for="exampleInput">Example Input</label> 
       <input type="text" name="exampleInput "id="exampleInput"> 
      </form> 
     </div> 
     <!-- Custom content ends here --> 
<?php 
     // collect the html 
     $html = ob_get_clean(); 
     $content .= $html; 
     return $content; 
    } 

    // add the html to the_content 
    add_filter('the_content', 'customPageExtraContent', 20); // 20 priority runs after other filters so the content is appended. 

    // include page 
    get_template_part('page'); 

?> 

这工作,并增加了上面的HTML到the_content。

我想知道的是:

  1. 这是真正实现我想要做一个好办法吗?
  2. 如果是这样,有抽象的customPageExtraContent功能的一种方式,所以我可以用它在多个自定义的页面模板

目前,如果我创建另一个页面模板,我就必须定义一个新的功能,例如

function customPageExtraContent2($content){ 
    ... 
} 

我想要做的是有一个通用的函数,我通过额外的HTML为这个特定的模板。

所以,我可以在我的函数文件中的函数一样

function customPageExtraContent($content, $extraHTML){ 
    return $content.$extraHTML; 
} 

然后在我的任何自定义模板我可以调用这个函数。

我这样做的问题?我不能工作了如何参数传递给我的回调函数......如我希望能够做到这在我的自定义模板

add_filter('the_content', customPageExtraContent($content, $extraHTML), 20); 

任何人谁知道我胡侃关于报价有什么建议么?

感谢:d

回答

0

,如果你想在当前页面模板被称为不创建一个页面模板 - 你必须设置你要调用模板。使用template parts创建模板的一部分 - 然后在if/while声明中调用它。

来源: https://developer.wordpress.org/reference/functions/get_template_part/

+0

我*想*能够设置模板的CMS。 –

+0

如果我只包含模板部分,我需要使用条件,如 'if(is_page_template'CustomPageTemplateName')){ get_template_part('extra'); }' 我可以在我的基本页面模板上做到这一点。要在管理员中选择此模板,我可以创建一个自定义模板,除名称以外不包含任何内容... 但是,这看起来倒退了。 –

+0

好吧我完全迷失在你实际要求的东西中。通俗地说, –