WordPress has built-in functions to get categories and tags but how can you pull the ones related to a Custom Post Type?
What is it?
For Custom Post Types (CPT), Taxonomies are used with Terms. When you need to get a CPT defined Tags or Categories, you cannot use the WordPress’s built-in get_the_tags() or get_the_category() functions as it is separate.
For example, the Easy Digital Downloads (EDD) uses CPT therefore, we need to use the proper Taxonomy to get its related Terms.
How does it work?
Replace the Taxonomy variable by yours and it will construct the HTML for the current Single post. It can be adapted to an Archive page by adding it to the Post Loop and assigning the post_id
The Code looks at Taxonomy -> Terms
Demo
//This can be put inside Single.php
//It will return the Download Tags for EDD
$taxonomy = 'download_tag'; // change this to your taxonomy
$terms = wp_get_post_terms( get_the_ID(), $taxonomy, array( "fields" => "ids" ) );
if ($terms) {
$html = '<ul class="list-inline">';
$terms = trim( implode( ',', (array) $terms ), ' ,' );
$html .= wp_list_categories( 'echo=&false&title_li=&taxonomy=' . $taxonomy . '&include=' . $terms);
$html .= '</ul>';
echo $html;
}
Comments are closed