Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

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

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?

Thanks

share|improve this question
    
Add some information/code about your actual query/loop and the template file(s) you're using to achieve this. So far one pretty much has to guess your approach. –  ialocin Dec 17 '13 at 12:38
    
Please follow up on the questions you asked, it's an important part of the process on WPSE - see What should I do when someone answers my question? and Why is voting important? for a deeper insight. –  ialocin Apr 8 at 13:30
add comment

2 Answers

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.

share|improve this answer
    
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. –  speedypancake Dec 16 '13 at 20:48
add comment

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){
        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
        }   
    }
}

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

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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