WordPress Snippets at WPcustoms

How to display current post taxonomies and terms

display all the taxonomies of the current post with attached terms


/**
 * Snippet Name: How to display current post taxonomies and terms
 * Snippet URL: https://wpcustoms.net/snippets/display-current-post-taxonomies-terms/
 */
  // get taxonomies terms links
function custom_taxonomies_terms_links() {
    global $post, $post_id;
    // get post by post id
    $post = &get_post($post->ID);
    // get post type by post
    $post_type = $post->post_type;
    // get post type taxonomies
    $taxonomies = get_object_taxonomies($post_type);
    $out = "
    "; foreach ($taxonomies as $taxonomy) { $out .= "
  • ".$taxonomy.": "; // get the terms related to post $terms = get_the_terms( $post->ID, $taxonomy ); if ( !empty( $terms ) ) { foreach ( $terms as $term ) $out .= ''.$term->name.' '; } $out .= "
  • "; } $out .= "
"; return $out; } // usage in your template: // echo custom_taxonomies_terms_links();