Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.