Skip to main content
6 of 6
Commonmark migration

I think what you could do is set up a if/else statement in your index.php that calls the single .php files as template parts. Importantly you would want to put the joint condition ( blue and red) as the first in the list of conditions. Note that the you have names you call in get_template_part need to match the slug associated with the files you want to retrieve. If your single page's php file is single-red.php the slug would be just 'single-red' , without the .php.

To control three possibilities

if ( in_category('red') && in_category('blue') ) {
     get_template_part( 'single-red' ); //both
} elseif ( in_category('red') ) {
    get_template_part( 'single-red' );  //just red
} else {
    get_template_part( 'single-blue' ); // just blue
    
}

or even more elegantly, if you don't care about keeping track of just reds:

To control only two possibilities

if ( in_category('red') ) {
     get_template_part( 'single-red' ); // just red OR blue and red
} elseif ( in_category('blue'){
    get_template_part( 'single-blue' ); //just blue
    
}

Please be aware that you should tailor your if statement to handle the categories you want it to analyze. If you only have categories 'red' and 'blue', then you don't need a final 'else'. But if you have posts that are neither 'red', nor 'blue' and you want them to be styled differently from red and blue, you might want to do something like what I show below. Note that you would need a third php file, called with a slug 'single' (like single.php), for this to work:

To handle a third default category, different from the other two

if ( in_category('red') ) {
     get_template_part( 'single-red' ); // just red OR blue and red
} elseif ( in_category('blue'){
    get_template_part( 'single-blue' ); //just blue
} else {
    get_template_part( 'single' ); // default single
    
}

Also note that that I only used single-parameter if-statement conditionals in my code above for 'in_category'. What I mean to say is that in the function "in_category", I only entered the category, either 'red' or 'blue'. This single-parameter use is fine if you are "the loop", but if you are not in the loop, you might need to pass the posts into the function 'in_category' using the approach The Codex suggests, using two parameters. So, if you aren't in the loop you would want to make sure you a have a variable storing your post (e.g. $post), and then use something like this in_category('red', $post); or in_category('red', $post->ID); instead of simply in_category('red').

I hope that gets your closer! Please try that and let me know if you run into any issues!