0

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?

1 Answer 1

4

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.

2
  • Shouldn't that be in_category? Commented Nov 11, 2012 at 10:09
  • Uhm, yes. :) And a post object should be passed too. Fixed.
    – fuxia
    Commented Nov 11, 2012 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.