Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display only first category of custom taxonomy WordPress


Hi All,

I've created a custom taxonomy (categories) to organise my custom post type (news). In my theme, I'd like to display what category a post belongs to.

Simple enough using get_the_term_list I know, but my problem is that some posts are assigned to multiple categories and I only want to show one of them. Just the first category it was assigned to is fine.

I used to do this like so using the built-in category system but I can't figure out how to do it with my custom taxonomy.
<?php $category = get_the_category(); echo $category[0]->cat_name; ?>

Any ideas? Thanks.

Answers (1)


John Cotton answers:

You could try

$categories= wp_get_object_terms($post->ID, 'categories');
echo $categories[0]->name;

oh_hello comments:

That appears to do the trick. Thanks.