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:

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.

function display_taxonomy_terms($post_type, $display = false) {
	global $post;
	
	$term_list = wp_get_post_terms($post->ID, $post_type, array('fields' => 'names'));

	if($display == false) {
		echo $term_list[0];
	}elseif($display == 'return') {
		return $term_list[0];
	}
}

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

// Echo the term name
<?php display_taxonomy_terms('YOUR_TAXONOMY'); ?>

// Return the term name
<?php display_taxonomy_terms('YOUR_TAXONOMY', 'return'); ?>

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.

function display_taxonomy_terms($post_type, $display = false) {
	global $post;
}

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.

$term_list = wp_get_post_terms($post->ID, $post_type, array('fields' => 'names'));

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.

if($display == false) {
	echo $term_list[0];
}elseif($display == 'return') {
	return $term_list[0];
}

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

// Echo the term name
<?php display_taxonomy_terms('YOUR_TAXONOMY'); ?>

// Return the term name
<?php display_taxonomy_terms('YOUR_TAXONOMY', 'return'); ?>

Comments

Leave a Comment

Comments (7)

andersonenvy

September 10, 2018 at 3:34 pm

It works, but how to HREF link to the Term archive?

Reply

Nicole

May 13, 2017 at 5:55 pm

Hello! Looking for further help on this! This code is not working for me. Thanks

Reply

Henry

March 5, 2017 at 5:41 pm

This is awesome! Just a quick question, is there anyway to make the output links? Thank you!

Reply

Ethel Palomino

September 4, 2016 at 10:18 am

Thank you! In fact i was trying to filter WP_query by a term but it was impossible with ‘terms’ => get_queried_object()->term_id., I don’t know why.
I could with your function. thank you!!

Reply

Alexandre Michel

May 9, 2016 at 3:08 pm

Hello, thanks for your snippet of code. However, it only echoes one of the three terms that are checked on my WordPress page. What am I missing? 🙂

Reply

Carlos

February 23, 2016 at 12:05 am

You are awesome, thanks for this code. Very useful.

Reply

Inspirational Newsletter


Join the newsletter to get the best articles, tutorials and exclusive freebies every two weeks.

No spam. You can unsubscribe at any time.