WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have categories like gaming and movies on my website and when a user visits any of these categories the content in the sidebar is displayed depending on the category they are in. So gaming content for the gaming category, movie related content for movies.

When a user selects a post in either of these categories, I have it defaulting to the sidebar that displays on the home page since I don't know how to keep the sidebar displaying the same content that is on the category pages.

The closest I could get would be something like <?php if ( is_single() ) : ?>. How would I add is_category( 'gaming' ) so it can target a single page (single.php) that belongs to the category gaming?

share|improve this question
up vote 3 down vote accepted

You can combine conditions:

if ( ! empty ( $GLOBALS['post'] )
   && is_single()
   && in_category( 'gaming', $GLOBALS['post'] ) 
   // do something

See PHP manual: Logical Operators and Exclude custom function content from certain pages.

share|improve this answer
Shouldn't that be in_category? – TheDeadMedic Nov 11 '12 at 10:09
Uhm, yes. :) And a post object should be passed too. Fixed. – toscho Nov 11 '12 at 10:14
@toscho thank you! – Joe Bobby Nov 11 '12 at 14:58

Your Answer


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.