【问题标题】:Wordpress 4.5.3: get content with applied template file?Wordpress 4.5.3:使用应用的模板文件获取内容?
【发布时间】:2016-08-04 17:38:40
【问题描述】:

有没有办法获取整个 wordpress 页面的内容?我的问题是,我想在另一个页面上包含页面内容(以创建单页布局)。我尝试的是这样的:

$post = get_post($the_page_id);
$content = apply_filters('the_content', $post->post_content);
echo $content;

但是页面有自己的模板,我想显示整个页面,包括在 template.php 文件中所做的任何事情。那可能吗?

【问题讨论】:

  • 包含整个模板也会再次包含页眉和页脚...这不仅无效,而且我很确定这不是您真正想要的。
  • 您可以将模板拆分为多个文件,然后使用get_template_part() 包含这些文件。

标签: php wordpress


【解决方案1】:

好吧,我想出了一个解决方案。这可能不是最好的方法,但这对我有用。我创建了一个页面“主页”来表示一页页面。此页面的所有子页面都将有一个不包含页眉或页脚的特定模板。我的主页模板如下所示:

//fetch header as usual
get_header();

//fetch all subpages of this page
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC'));
$children = get_page_children( get_the_ID(), $all_wp_pages );

//browse these subpages and get the content for each one 
foreach($children as $child){
    $post = get_post($child->ID);
    getOnePageContent($post);
}

//footer
get_footer(); 

现在在functions.php中我定义了getOnePageContent:

function getOnePageContent($page){
    global $post;
    $post = $page;
    $slug = get_page_template_slug( $page->ID );
    if($slug){
        $pl = get_the_permalink($page);
        $content = file_get_contents($pl);
        echo $content;
    } else {
        $content = apply_filters('the_content', $page->post_content);
        echo $content;
    }
    wp_reset_postdata();
}   

只要子页面不包含页眉或页脚,就可以正常工作。当然,我不认为使用 file_get_contents 获取页面是一个优雅或好的解决方案,但至少它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-07
    • 2021-03-08
    • 1970-01-01
    • 2015-05-27
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多