0

I have been working on this loop for a very long time. I think so far this is my best version as it will get page templates. My problem is that it does not get posts and I am not sure why. When I try to get the posts it will display the homepage template about 11 times.

 $page_query = new WP_Query(array(
    'post_status' => 'publish',
    'post_type' => array( 'post', 'page' ),
    'order' => 'ASC',
    'orderby' => 'menu_order',
    'posts_per_page' => '-1',
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));

if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); 

//checking if there is a page template assigned to the page
$template = get_post_meta( $post->ID, '_wp_page_template', TRUE );

 //if there is a template and it's not the default or homepage template
 if( $template && $template != 'template-home.php' && $template != 'default') { 

//here we remove ".php"
$template = str_replace('.php','',basename(get_page_template()));

    //including the template the correct way
get_template_part('template', $template); 
}
 endwhile;
 endif; 

Any attempt to include posts which should be a part of the $page_query array fail. I get bad results.

I have tried including category.php, get_template_part('content', get_post_format()). It just gives me messed up stuff. Any ideas?

  • I can see that getting templates could cause multiple loops. Have you tried using rewind_posts() outside of the while statement? codex.wordpress.org/The_Loop#Multiple_Loops – Nathan Powell Apr 15 '14 at 15:37
  • That is an interesting thought. I did not try that. this isn't really a multiple loop issue though. The loop is suppose to get posts and pages. I would think right outside the if statement for the template I should be able to get posts. Just not sure how to do that. – Jamie Apr 15 '14 at 20:09
  • I suggest posting the code for the templates before anyone can really debug. You are trying to pull multiple templates onto one page and that could be causing the complications. – Nathan Powell Apr 16 '14 at 14:20
  • I think I am going to inlcude posts another way. After thinking about it. It might cause problems for me to get posts. – Jamie Apr 17 '14 at 0:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.