Display Current Post Taxonomies and Terms in WordPress

In this tutorial, i am gong to show you how to display current post taxonomies and terms in a list. Please open your function.php file and paste the below code:

<?php 
// 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 = "<ul>";
    foreach ($taxonomies as $taxonomy) {        
        $out .= "<li>".$taxonomy.": ";
        // get the terms related to post
        $terms = get_the_terms( $post->ID, $taxonomy );
        if ( !empty( $terms ) ) {
            foreach ( $terms as $term )
                $out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> ';
        }
        $out .= "</li>";
    }
    $out .= "</ul>";
    return $out;
} ?>

Now we will use the function to display this. Where you want to display the call the below function:

<?php echo custom_taxonomies_terms_links();?>

Demo output

The output might look like this if the current post has the taxonomies country and city:

<ul>
    <li>  country:  
          <a href="http://example.com/country/denmark/">Denmark</a> 
          <a href="http://example.com/country/russia/">Russia</a> 
    </li> 
    <li>   city:  
           <a href="http://example.com/city/copenhagen/">Copenhagen</a> 
           <a href="http://example.com/city/moscow/">Moscow</a> 
    </li> 
</ul>

 

This post has already been read 4070 times!

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

More Posts

Mehedi Hasan

Cool WordPress Developer having much agile experience to develop any kind of WordPress sites & plugins. Also good in troubleshooting, fixing & making any kind of tweaks for WP site.

3 thoughts on “Display Current Post Taxonomies and Terms in WordPress

  • December 3, 2014 at 5:29 pm
    Permalink

    That’s awesome Mehedi !! I was searching few hours to get this example of code. Thanks

    Reply
  • July 1, 2016 at 10:06 pm
    Permalink

    Perfect, a good way to channel steel. I was looking for something, in the codex and other tutorials wordpress always asked the ‘taxomonies’ but I needed to know the taxonomies within a post. By this I give good results

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *