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 am trying to use this code to display something in the 'single.php' file on the theme when the post is of a certian catagory.

$cat = get_query_var('cat');
if ($cat == '4') {
echo "post";
} else {
echo "no data";
}

But it seems to ignore the $cat var query and as such on every post display the no data message.

The cat var is not in the url.

share|improve this question
add comment

1 Answer

get_query_var('cat'); only returns the category id in a category archive page;

in a single post, try to use, for instance:

wp_get_post_categories()

example:

if( in_array(4, wp_get_post_categories($post->ID)) ) :

http://codex.wordpress.org/Function_Reference/wp_get_post_categories

share|improve this answer
    
That works a treat. –  Peter Jul 26 '11 at 13:36
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.