Wordpress 4.5.3:获取应用模板文件的内容?

Wordpress 4.5.3:获取应用模板文件的内容?

问题描述:

Is there a way to get the content of an entire wordpress page? My problem is, I want to include page content on another page (to create a one-page layout). What I tried was this:

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

But the page has its own template and I would like to display the entire page, including whatever is done in the template.php file. Is that possible?

Well, I came up with a solution for this. It might not be the best way but this worked for me. I created a page "home" to represent the one-page page. All subpages of this page will have a certain template which does not include header or footer. My home template looks like this:

//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(); 

Now in functions.php I defined the 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();
}   

As long as the subpages do not contain headers or footers this works fine. Of course, I do not think it's an elegant or good solution to fetch the page with file_get_contents but at least it works.