Accessing Custom Taxonomy of a Custom Post Type in the_loop

I was wondering how to access the custom taxonomy of my custom post types so it can be mentioned in the_loop. To continue my example from earlier posts, I have custom post type of “game”, with a custom taxonomy of “platform”. When I list a game, I’d like to show the platform in a list after its title.

On regular posts, I have access to access to both categories and tags using the get_the_category() or get_the_tags() functions. For my project I have a custom query for the custom post type, and I’m using a custom taxonomy on it. I’d like to find out the name of that custom taxonomy. How do we do that?

We’ll use get_the_terms(), which is used by WordPress for both the above functions under the hood. Here’s how we can display all taxonomies of one type (must be used in the_loop):

foreach (get_the_terms(get_the_ID(), 'platform') as $platform){
  echo $platform->name;
}

Thanks to Ankur Bhadiania for this tip.

Jay is founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or drawing on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.

You can leave a comment on my original post.