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

1 Answer 1

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

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



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


share|improve this answer
That works a treat. –  Peter Jul 26 '11 at 13:36

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.