1

My posts can have any of 2 categories: 1. red 2. blue

I have 2 different category pages category-red.php category-blue.php

A post can be in both red and blue category

If a posts opens from the red category I want it to have a single-red style even if it also belongs to the blue category.

is that possible?

0

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!

0

Seems to me you need to signal to the single template which category archive sent the use and change the class of an element on single (e.g. body class).

The main possibilities that come to my mind is either

  • add a variable to the link on category archives, single reads it and assign the proper class
  • single gets the referrer from post request and based on it, assigns the proper class

In the first case you could make use of WordPress add_query_arg and get_query_var functions to create and read a custom query variable.

In the second case, you could make use of wp_get_referer, wp_get_original_referer or even PHP [$_SERVER\['HTTP_REFERER'\]]5

Mind that making the HTML markup changing like this using only PHP makes harder to cache the page. You could experiment using a javascript only approach, by checking [document.referrer]6 (which will tell the origin URL, that you can parse) and appplying the class using the script.

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.