I have differnet Custom Post Types in my blog.
I also have custom templates for each of them like loop-{$cpt-name}.php
I would like to include different cpt templates dynamically in search results based on what custom post type is post at the moment in the loop. I am trying to do it like this ( search.php ):
<?php
while ( have_posts() ) : the_post();
if ( get_post_type ( $post->ID ) == 'teams' ) {
get_template_part( 'loop', 'teams' );
} elseif ( get_post_type ( $post->ID ) == 'players' ) {
get_template_part( 'loop', 'players' );
} else {
get_template_part( 'loop', 'search' );
}
endwhile;
?>
However, the problem is that when I am trying to do it like so, I have infinitive loop in a query. Because in search.php file I check the post type in the while loop and include the template where I also have while statement ( I need to have it there ). Example of my CPT template ( beginning ):
<?php
/**
* The loop that displays teams
*
*/
if ( have_posts() ) while ( have_posts() ) : the_post();
I came up with the idea to make additional query for each found post in the loop, like so:
while ( have_posts() ) : the_post();
if ( get_post_type ( $post->ID ) == 'teams' ) {
query_posts('p='.$post->ID);
get_template_part( 'loop', 'teams' );
wp_reset_query();
But I suppose that would slow down the search in times. ( have not tested yet)
Any suggestions will be appreciated.