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.

How can I identify what category the current page is showing in the <head>? Here's the code I have now:

<?php if ( is_home() ) { ?>
    <!-- set up for home page ad -->
<?php } elseif ( in_category( 'Restaurant Reviews' )) { ?>
    <!-- set up for restaurant reviews ad -->
<?php } else { ?>
    <!-- set up for run of site ad -->
<?php } ?>

This code fails on my "Restaurant Reviews" page... it always selects the "run of site" option.

As a test, lower down in the page, I used the_category() to see what categories WP thought the current page used, and I got "Chelsea, Manhattan, New York, Restaurant Reviews". And of course, the in_category() command works correctly there in the lower part of my page, and properly recognizes when it's in the "Restaurant Reviews" category.

So-- up there in the <head>, how can I tell when I'm in the "Restaurant Reviews" category?

share|improve this question
    
Really, one downvote and no comment or answer? Sheesh, tough crowd... –  Eric Feb 19 '13 at 23:07
1  
Yeah, it can be at times. :) The downvote might be because the question is fairly easily answered via the Codex. But personally, I think confusing is_category() and in_category() is easy to do and common, and as such, this is a good question. –  Chip Bennett Feb 20 '13 at 2:09
add comment

2 Answers

up vote 1 down vote accepted

The in_category( $category ) conditional only returns true when the current post has the specified category assigned. If you want to determine when you are on a category archive index page, you should instead use is_category( $category ):

if ( is_category( 'Restaurant Reviews' ) ) {
    // The current page is the Restaurant Reviews
    // category archive index page; do something
}

Side note: is_home() returns true when the current page is the blog posts index, which will not always be true for the site front page. If the site uses a static front page, the blog posts index will be on a separate, static page, and is_home() will not return true on the site front page. So, if you actually intend to query for the site front page, use is_front_page():

<?php 
if ( is_front_page() ) {
    // set up for home page ad
} elseif ( is_category( 'Restaurant Reviews' )) {
    // set up for restaurant reviews ad
} else {
    // set up for run of site ad
} 
?>
share|improve this answer
add comment

It depends on where you are in the <head>. Take a look at the Plugin API/Action reference. The post object is not set up until the wp hook. If you are adding this conditional, manually the header.pher before wp_head() you probably don't have anything to work with.

I'd try adding it via function to perhaps even the wp_enqueue_scripts hook.

share|improve this answer
    
Actually, the query is already set, and all query conditionals known and available, by the time the template loads (after all, template_redirect relies on knowing the result of template_loader to determine the correct template to load from the template hierarchy - and the template hierarchy depends on the query conditionals. –  Chip Bennett Feb 20 '13 at 2:11
    
Good point. Thanks for the correction. –  helgatheviking Feb 20 '13 at 3:12
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.