是否将模板页面包括在另一个模板中?

时间:2013-12-15 作者:speedypancake

我创建了一个垂直页面模板,它显示堆叠的部分(这些部分只是来自其他wordpress页面的内容)。我在显示使用模板的页面内容时遇到问题。

这是我用来获取其他页面内容的主要查询:

<?php
//change to the title of the pages you want to feature
$page = get_page_by_title( \'About\' );
$page_data = get_page($page);

// Displays the content
echo apply_filters(\'the_content\', $page_data->post_content);
?>
除了那些依赖模板本身的页面外,这对大多数页面都很有效。我“认为”我需要包括一些关于“获取模板部分”的内容,但我没有足够的经验来理解如何在这种情况下使用,是否需要一些帮助?

谢谢

2 个回复
SO网友:Nicolai Grossherr

功能get_page() 已经deprecated, 不要再使用它了,使用get_post() 相反get_page_by_title() 默认情况下返回一个对象,您可以通过更改$output 参数,您可以像以前一样使用它。虽然get_post() 可以将对象作为$id 参数我个人更喜欢输入ID,但这实际上并不重要。

$page = get_page_by_title( \'About\' );
$page_data = get_post( $page->ID );

echo $page_data->post_content;
您所描述的内容(至少基于您问题中的代码)应该与模板或get_template_part() 函数,因为post_content 你变得独立了。如果你所谈论的那些页面中缺少了什么,那么情况当然会有所不同isn\'t 实际上是post_content, 但你的问题并没有表明这一点。

SO网友:Madalin.Ungureanu

我的示例以导航菜单中的页面为例:

$menu_id = 2; //insert your menu id here
$menu_items = wp_get_nav_menu_items($menu_id);
if( !empty( $menu_items ) ){
    foreach ($menu_items as $menu_item){
        wp_reset_postdata();
        if ($menu_item->object == \'page\'){
            $post = get_page($menu_item->object_id);
            setup_postdata($post);          
            $template_name = get_post_meta( $post->ID, \'_wp_page_template\', true );
            $template_name = str_replace(\'.php\', \'\', $template_name);
            if($template_name == \'default\') $template_name = \'tpl_default_page\'; 
            ?>
                <div class="page-content">
                <?php get_template_part($template_name); ?>     
                </div>
            <?php
        }   
    }
}
此外,您的模板不得get_header()get_footer() 在他们身上。

结束

相关推荐

Custom post types templates

我已经注册了一个名为“Services”的自定义帖子类型,但当我给这篇帖子提供模板“single Services.php”时,它不接受这个,而是选择了“homepage.php”模板,你能告诉我这个问题吗?它也没有使用“archive services.php”模板,而是使用blog模板(index.php)使现代化代码如下:add_action(\'init\', \'services_register\'); function services_register() { $