WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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.