Chi Chi - 1 year ago 76
PHP Question

Wordpress 4.5.3: get content with applied template file?

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?

Chi Chi
Answer Source

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

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


Now in functions.php I defined the getOnePageContent:

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

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download