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 need to display some HTML only if the post being displayed has a category - not a specific category, just any category at all.

I tried if (!is_empty(get_the_category($post->ID))), but for some reason this isn't working. Has anone else had the same issue?

share|improve this question
up vote 6 down vote accepted

Use has_category instead.

if (has_category('',$post->ID)) ...

If you want to use it in The Loop, you don't need to specify the ID.

if (has_category()) ...

share|improve this answer
This is giving me an error for some reason. The code I'm using is: <?php if (has_category()) { ?>Posted in <?php the_category(', ') ?> | <?php } ?>. It's weird this is causing an error because I'm using identical syntax for the next line (<?php if (is_single()) { ?><a href="<?php comments_link(); ?>"><?php comments_number('0 Comments', '1 Comment', '% Comments'); ?></a><?php } ?>) and that displays fine. – daysrunaway Aug 2 '11 at 17:42
Are you running WordPress 3.1 or above? has_category was added in WP 3.1. – Otto Aug 2 '11 at 19:15
No, I'm running WordPress 3.0.4; it's for a large and poorly engineered site that I've inherited from another developer, so I'm reluctant to upgrade for fear of breaking something. – daysrunaway Aug 2 '11 at 19:18
Welp, there's your problem. Try using is_object_in_term( $post->ID, 'category') instead. Or better yet, upgrade. Seriously, running old code sucks rocks. – Otto Aug 2 '11 at 19:20
Agreed, it's just not an option given the time allotted to the project. – daysrunaway Aug 2 '11 at 20:07

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.