0

I will have around 5 custom post types on a new site. I am looking for an efficient way to display these post types on one (index) page, assigning a different template / style to each one (for example one post type might need a featured image, others might be quotes etc.)

This older post response by danblaker looks like it is doing exactly the right thing without needing to write five or more loops for one page, but being fairly new to WP I cannot see exactly how and where to apply it or indeed if this is even possible still!

So far I can display a given post type one at a time on the index page and I can assign a specific template to this post type one at a time like so —

$args = array( 'post_type' => 'atoz_key', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();

get_template_part( 'template-parts/atoz_key', get_post_format() );

endwhile;

ABOVE the main index loop, but doing this 5+ separate loops seems bit daft!

Dan's solution seems much more elegant but I cannot see how exactly to implement!

//

Any help much appreciated.

thanks!

Matt

  • 1
    How do you want them ordered? Do you want each post type grouped, or mixed and ordered by date? – Milo Sep 18 '17 at 1:56
  • @Milo I don't think this question is related to post type grouping or ordering. – CodeMascot Sep 18 '17 at 3:27
  • @the_dramatist it isn't specifically about ordering or grouping, but the answer will differ in the case where they want the post types mixed. Your answer below assumes they want each post type output individually, an assumption which is not supported by the information currently provided. – Milo Sep 18 '17 at 13:23
  • In this case I am happy for the post types to be output one set at a time in a 'sandwich' structure (!) — so don't need to be mixed. Mind you it would be interesting to see how to output things mixed! – dezbat Sep 22 '17 at 21:58
1

You better look at the second answer at the post you gave link. Use content-{post-type}.php to name all the template for each post type (If you want to know how you have to break down the template to pieces of content-{post-type}.php, then please have look at the Twenty Sixteen theme's template-parts directory and analyze it's template architecture). And call them like below at your page template-

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;

This way you can get the post type of current post by get_post_type( $post ) and assign it to the content related to the post type. And for your loop, it'll look kinda like below-

$your_post_types = array(
    'atoz_key',
    'post_type_2',
    'post_type_3',
    'post_type_4'
);

foreach($your_post_types as $p_type ) {
    $args = array(
        'post_type' => $p_type,
        'posts_per_page' => 10
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        get_template_part( 'template-parts/content', $p_type );
    endwhile;   
}
  • this is working well so far, really helpful... will post a working example once I have my different post-types functioning... – dezbat Sep 26 '17 at 20:05

Your Answer

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

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