Check if post belongs to any category

In my homepage loop, I have something that will show which category the post is in. It will say something like “Posted 4 hours ago in Sports by …”. However, when there is no category set, it will omit the category (obviously) and just say “Posted 4 hours ago in by …”. My question is: how do I check if a post belongs to any category in WordPress.

I know that the function in_category() exists, but I would like to avoid having to type out all of the categories, in case they change, or whatever.

Thanks!

3 Answers
3

You can use is_object_in_term(). Despite name it can also check for taxonomy match if terms are omitted.

is_object_in_term( get_the_ID(), 'category' );

Leave a Comment