基于另一个(父)模板创建Wordpress模板

基于另一个(父)模板创建Wordpress模板,wordpress,wordpress-theming,custom-wordpress-pages,Wordpress,Wordpress Theming,Custom Wordpress Pages,我正在尝试基于基本页面模板创建一个新的页面模板,自定义页面 我想在自定义页面模板中做的是创建一些HTML,定义将其添加到基本页面模板的位置,然后只包含基本页面模板 我不想做的是将页面模板的内容复制粘贴到我的自定义页面模板中,然后修改它。为什么?因为它有助于提高可维护性——我希望将来能够修改基本页面模板,并将任何更改自动应用于我的自定义页面模板 以下是我迄今为止在自定义页面模板中得到的示例: <?php /* Template Name: Custom Page */ //

我正在尝试基于基本页面模板创建一个新的页面模板,自定义页面

我想在自定义页面模板中做的是创建一些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传递给它

所以我可以在我的函数文件中有一个函数

function customPageExtraContent($content, $extraHTML){
    return $content.$extraHTML;
}
然后在我的任何自定义模板中,我都可以调用这个函数

我这样做有问题吗?我不知道如何将参数传递给回调函数。。。e、 g我希望能够在我的自定义模板中执行此操作

add_filter('the_content', customPageExtraContent($content, $extraHTML), 20);
有谁知道我在胡说什么吗?有什么建议吗


谢谢:如果希望调用当前页面模板,请不要创建页面模板-您必须设置要调用的模板。使用
template parts
创建模板的一部分,然后在
if/while
语句下调用它

资料来源:

我希望能够在CMS中设置模板。如果我只包括模板部分,我需要使用一个条件,例如
If(is_page_template'CustomPageTemplateName'){get_template_part('extra');}
我可以在我的基本页面模板上这样做。要在管理员中选择此模板,我可以创建一个自定义模板,其中除了名称之外没有其他内容。。。但这似乎是倒退。好吧,我完全不明白你到底在问什么。用外行的话说?哈哈,对不起!我希望CMS中的用户能够选择自定义页面模板。此自定义页面模板与基本页面模板相同,只是有一些附加内容,例如联系人表单。这使用户能够选择任何给定页面是使用基本页面模板,还是添加了联系人表单的基本页面。因此,基本上我想要一种定义自定义页面模板的方法,并在其中说“使用基本页面模板,但在Y位置插入X个额外的html”。有意义吗?我明白了-所以你只需要自定义字段?查看-通过这种方式,您可以将自定义字段添加到已选择的模板中。例如,添加图像、表单等。我仍然相信在这种情况下,使用get_template_部分并选择其调用方式会很好。
add_filter('the_content', customPageExtraContent($content, $extraHTML), 20);