At Vatu Ltd, we have 3 main goals: Create high-quality websites Give great customer service to our clients Create a happy, safe,...
Simple way to get a single post’s taxonomy
Get to know the get_the_terms() function
Darren PinderSo you’ve got a custom taxonomy, and you want to return the taxonomy’s name(s) (“terms”) on a single post template on the front-end.
For example, we have a post type called Careers. Each individual post has a taxonomy called Location (with the slug ‘location’). This taxonomy has multiple terms (in the form of a checkbox list which supports multiple selections).
Try this:
// Get all the current post's terms from the taxonomy taxonomy_name. This can be multiple terms.
$terms = get_the_terms( get_the_ID(), 'taxonomy_name' );
// If terms were found
if ( $terms ) :
// Create an empty array to be filled
$locations = array();
// Loop each of the terms found within the $terms array
foreach ( $terms as $term ) {
// Add the term name to the $locations array
$locations[] = $term->name;
}
// Join all the array items into a string using join()
$output_locations = join( ", ", $locations );
endif;
You must be logged in to post a comment.