• Resolved aasemjs

    (@aasemjs)


    Hey there hope you are doing well!

    I have run into a bit of a situation. There are 4 categories on my site. For category 1 and 2, I want to use the post template defined in my ‘content-single.php’.

    And for the other two categories, I want to be able to use a different template, for example, ‘content-new.php’.

    This is my code in the single.php file (The template for displaying all single posts.)

    <?php
    while ( have_posts() ) : the_post();

    get_template_part( ‘template-parts/content-single’, get_post_format() ); ?>

    <section class=”white-background”>

    <?php // If comments are open or we have at least one comment, load up the comment template.
    if ( comments_open() || get_comments_number() ) :
    comments_template();
    endif;

    endwhile; // End of the loop.
    ?>

    // My site is at technobyte.org

Viewing 4 replies - 1 through 4 (of 4 total)
  • Eric Amundson

    (@sewmyheadon)

    Hey aasemjs,

    You could probably approach this in a few ways, but I’d be most inclined to add conditional logic to the single.php template that looks to see what category the post is in and includes a template_part that jives with that category’s desired layout.

    Maybe something like this:

    <?php
    
    while ( have_posts() ) : the_post();
    
    if ( in_category( array( 1,2,3 ) ) { // if post is in_category ids 1, 2, or 3 (sub your cat IDs)
    
    	get_template_part( 'template-parts/content-single', get_post_format() ); 
    
    } else {
    
    	get_template_part( 'template-parts/content-new', get_post_format() ); 
    
    } ?>

    Make sense?
    Eric

    Thread Starter aasemjs

    (@aasemjs)

    Please call me Aasem.

    And wow that does make sense. I can’t believe I didn’t think of it.
    Thanks!

    Thread Starter aasemjs

    (@aasemjs)

    Can you help me out with another thing? How would I go about fetching posts from 2 categories and not all 5 of them?

    Eric Amundson

    (@sewmyheadon)

    Hey Aasem,

    How would I go about fetching posts from 2 categories and not all 5 of them?

    In the snippet I posted earlier, change the numbers listed in the array below to match the IDs of the categories you’re looking to pull in.

    if ( in_category( array( 1,2,3 ) ) { // if post is in_category ids 1, 2, or 3 (sub your cat IDs)

    So, for example, if you have two categories:

    1. Lemons (Cat ID: 3)
    2. Peaches (Cat ID: 92)

    . . . your snippet would look more like this:

    if ( in_category( array( 3,92 ) ) { // if post is in_category ids 3, or 92

    I hope that helps!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to have multiple blogpost styles’ is closed to new replies.