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 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.

share|improve this question

2 Answers 2

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;
share|improve this answer

You're definitely better off only doing one query, but it will require a little re-architecting of your custom post type loops. Basically, you would move the presentation code out of the loops, into new files (which we'll call "partials"). Instead of loop-{$cpt-name}.php, you'd have a file partial-{$cpt-name}.php.

Instead of checking the post type from search.php, you'll check it in loop-search.php where you'll include the appropriate partials if they exist, like so:

<?php
while ( have_posts() ) : the_post(); 
 $cpt = $post->post_type; 
 $partial = "partial-{$cpt}.php";
 if ( locate_template( $partial )
    {
      include( locate_template( $partial ) );
    }
  else
    {
      ?>
      <div>Default Search loop output here for non-CPT posts</div>
      <?php
    }
endwhile;
?>

You would do the same thing in your regular loop, rather than using separate loops for each custom post type. If you want to keep using separate loops, you'd import the partials in those CPT loops instead of specifying the presentation code.

share|improve this answer
    
The idea was that I keep one file for one template, so I don't have to change multiple files ( templates ) when I have changed something in my output template. But anyway, thanks for reply. –  Godforever Jun 28 '12 at 9:40
    
Sorry, I was confused about your current approach—I missed where you were calling loop-search.php from the search.php template. I've edited my answer a bit to fix that; it may still be the wrong approach for you, but at least it answers the actual question. If you need different custom post type output for different contexts, then you could end up with search_partial-{$cpt-name}.php and index_partial-{$cpt-name}.php, which gets a little busy. I personally find it easier to look in the directory than in the loop code when it's time to change the output. –  danblaker Jun 28 '12 at 21:15

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.