I've created a vertical page template which displays stacked sections (which are simply the content from other wordpress pages). I am having problems displaying the contents of pages which are themselves using templates.

This is the main query I am using to fetch the contents of other pages:

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

This works fine for most of the pages, except those which rely on templates themselves. I "think" I need to include something about "get template part", but I am not experienced enough to understand how to use in this situation and could use some help?



2 Answers 2


The function get_page() has been deprecated, don't use it anymore, use get_post() instead. get_page_by_title() returns an object by default, you can change this by altering the $output parameter, which you can use like you did. Although get_post() can take an object as $id parameter I personally prefer inputing the ID, but that actually shouldn't matter.

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

echo $page_data->post_content;

What you're describing - at least based on the code in your question - should have nothing to do with templates or the get_template_part() function, because the post_content you're getting is independent from that. The case would of course be different from that, if what your are missing from those pages you talk about isn't actually part of the post_content, but your question isn't indicating that.

  • Thankyou very much for updating my code. Now using get post instead. I'm afraid you are correct about "if what you are missing from those pages isn't part of the post content". The templates I am having trouble with involve filling in details in metaboxes eg. list number of posts, categories, thumbnail size etc and DO NOT FORM PART OF THE WP EDITOR POST CONTENT.... Any way to capture the page as wordpress & the template renders it??? Thanks again. Dec 16, 2013 at 20:48

My example takes the pages from the navigation menu:

$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){
        if ($menu_item->object == 'page'){
            $post = get_page($menu_item->object_id);
            $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); ?>     

Also your templates mustn't have get_header() or get_footer() in them.

  • Thanks for the suggestion, but it didn't work (nothing displayed) on first go so will have to try again later :) Dec 16, 2013 at 21:04
  • Be sure to change the $menu_id var to your menu id Dec 16, 2013 at 21:09
  • Yeah - did that & removed the header / footer from the template - Cheers Dec 16, 2013 at 21:24
  • Is this working now? @speedypancake Dec 16, 2013 at 22:03
  • @ialocin - not yet... Dec 16, 2013 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.