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 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

1 Answer 1

up vote 4 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
1  
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
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 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

 
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.