Display Custom Taxonomy Terms in WordPress


When I first started messing around with custom taxonomies, along with custom post types, displaying the terms like I would the default categories or tags really tripped me up.

In this short tutorial my aim is to make it easy for anyone wanting to display custom taxonomy terms.

The Difference Between Taxonomies and Terms

Once you start getting into custom post types, taxonomies and all the rest, it can be quite confusing to know the difference between a taxonomy and a term.

If you’re unsure on what a custom “taxonomy” or “term” is, let me explain the difference:

  • Taxonomy: This is the collective name of all the terms together. It’s like saying “categories” or “tags”.
  • Terms: These are the individual categories or tags themselves. For example, “Personal Thoughts” or “Lifestyle”.

Final Code

The reason the $display variable is set to false is to make the default an echo, not a return. The reason the code below allows you to echo and return, is so you can use the function in an if-else statement.

To use the function, just input your custom taxonomy and whether you want it to return or echo, like so:

Display the Terms Walkthrough

The first thing to do is go into your functions.php file and create a new function called display_taxonomy_terms. Then add in the two variables we’ll be passing through.

You then need to call the $post variable, which is built-in to WordPress and contains information from the current loop.

Now you need to get the terms that are attached to the current post in the loop. You can do that with wp_get_post_terms. Take note that this is where the $post_type variable comes into play.

Lastly you just need a simple if-else statement to check what type of display to use, echo or return. The below states: If $display is equal to false, then echo it, if it’s equal to return, the return it.

Another way to do this is to just check for the “return” keyword and, if it’s false, echo the term with an else. However, I prefer this way as it more clearly states what’s going on.

All that’s left to do now is either echo or return your terms, like so:

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *