Include different loop templates in search query

I would recommend using content-{posttype}.php method, where you only have HTML markup, template tags for posts or custom post types and leave loops outside of those files. See Twenty Eleven for more detailed example.

After this change you can easily modify search.php and include specific content markup based on post type.

if ( have_posts() ) : 
    while ( have_posts() ) : the_post();    

    /**
     * Would include CPT content template (content-teams.php) if it exists
     * or content.php otherwise.
     */
    get_template_part( 'content', get_post_type( $post ) ); 

    endwhile;
endif;

Leave a Comment