I have a custom post type called products.

I've created a hierarchical taxonomy called archive categories. Within this taxonomy I've created top level and sub categories (image below)

enter image description here

What I would like to do is when viewing the single post page is to display the names of a selected sub category (date).

Ulitmately I want to display the CPT taxonomy name like: Date: 2001

I've looked into and tried wp_get_post_terms and get_terms but these results return all taxonomies, not ones associated with the post.

Any help much appreciated.



To return the list of terms attached to that post, try get_the_term_list(). This will return the terms for a single post and by using the before, separator and after arguments you can easily build an HTML markup structure to suit your needs.

The following example creates an unordered list of terms from the Topics taxonomy attached to a specific post:

$terms = get_the_term_list( $post->ID, 'topics', '<ul><li>', '</li><li>', '</li></ul>' );
if ( $terms ): ?>
  <h2><?php esc_html_e( 'Topics:', 'yourtextdomain' ); ?></h2>
<?php echo wp_kses_post( $terms, 'yourtextdomain' );

WP Codex entry for tget_the_term_list(): https://codex.wordpress.org/Function_Reference/get_the_term_list

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.