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?

1 Answer 1


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()) ...

  • 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. Commented Aug 2, 2011 at 17:42
  • 1
    Are you running WordPress 3.1 or above? has_category was added in WP 3.1.
    – Otto
    Commented Aug 2, 2011 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. Commented Aug 2, 2011 at 19:18
  • 1
    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
    Commented Aug 2, 2011 at 19:20
  • Agreed, it's just not an option given the time allotted to the project. Commented Aug 2, 2011 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.