
Output Taxonomies List for Custom Post Type by Post ID
This is a code snippet I used recently for a custom WordPress theme I built for a client.
The client was getting their site redesigned in part because they wanted to be found in Google, and in part because it was time for a re-branding.
This specific custom post type had it’s own category taxonomy. We needed to output a list of the CPT categories associated with a specific post by the Post ID.
In essence, this works like get_the_category_list
, but because we are using a custom taxonomy, we need a different function.
This is where get_the_term_list
comes into play.
I use Underscores as a base theme in many of my projects, so I add the following pluggable code snippet to the bottom of /inc/template-tags.php
.
In the following example, we have a custom post type with a slug of projects and the associated taxonomy project_category.
if ( ! function_exists( 'theme_entry_footer_cat_project' ) ) :
function theme_entry_footer_cat_project() {
// Get custom taxonomy project_category for each specific CPT
if ( 'projects' === get_post_type() ) {
echo '<span class="cat-links">';
echo get_the_term_list( $post->ID, 'project_category', '', ', ' );
echo '</span>';
}
}
endif;
You can use the function in your theme to output a list of taxonomies (special categories) associated with each post type.
This comes in handy for when you don’t want to rely on the built in taxonomies of Categories and Tags.
You can read more about how to use get_the_term_list
on the WordPress Codex.