How to get current term in my custom taxonomy in WordPress?

I need to display the current term in my custom taxonomy in a single post.

example:

Read More
  • My custom taxonomy is products and term of them is product-1, product-2 and products-3.
  • My post is assigned to product-2
  • And I want to print the current products = products-2 in my post

In fact, I need a function like WordPress’s the_category(); but for my taxonomy like the_customtaxonomy();

UPDATE :

in facts i know i need to get id of this becuse i need to show a icon for this in my single , for example a function like the_category_ID();

Related posts

Leave a Reply

7 comments

  1. You can use get_the_term_list():

    Description

    Returns an HTML string of taxonomy terms associated with a post and given taxonomy. Terms are linked to their respective term listing pages.

    Usage

    <?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?>
    
  2. tanks for answer from my friend , i find it for show slug of my taxonomy

    <?php
     $terms = get_terms('my-taxonomy-name');
     foreach ( $terms as $term ) {
     echo $term->slug.' ';
     }
    ?>
    

    and
    but it return all term in my taxonomy and i need to return current term in my taxonomy ..

    UPDATE :

    i finaly find this and add if for empty terms and works

    <?php   // Get terms for post
     $terms = get_the_terms( $post->ID , 'oil' );
     // Loop over each item since it's an array
     if ( $terms != null ){
     foreach( $terms as $term ) {
     // Print the name method from $term which is an OBJECT
     print $term->slug ;
     // Get rid of the other data stored in the object, since it's not needed
     unset($term);
    } } ?>
    
  3. I found it:

    <?php 
    //list terms in a given taxonomy using wp_list_categories (also useful as a widget if using a PHP Code plugin)
    
    $taxonomy     = 'genre';
    $orderby      = 'name'; 
    $show_count   = 0;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no
    $title        = '';
    
    $args = array(
      'taxonomy'     => $taxonomy,
      'orderby'      => $orderby,
      'show_count'   => $show_count,
      'pad_counts'   => $pad_counts,
      'hierarchical' => $hierarchical,
      'title_li'     => $title
    );
    ?>
    
    <ul>
    <?php wp_list_categories( $args ); ?>
    </ul>
    

    It gets all terms in my custom taxonomy and I need to get current term.

  4. Taking what user3208 coded, I have added a bit of code that adds the URL to the Term. Hope that helps someone out.

    <?php   // Get terms for post
    $terms = get_the_terms( $post->ID , 'oil' );
    // Loop over each item since it's an array
    if ( $terms != null ){
    foreach( $terms as $term ) {
    $term_link = get_term_link( $term, 'oil' );
     // Print the name and URL
    echo '<a href="' . $term_link . '">' . $term->name . '</a>';
    // Get rid of the other data stored in the object, since it's not needed
    unset($term); } } ?>
    
  5. A bit of update on the subject as this question is about 11 years old, and nobody mentioned it.


    You can use get_query_var().

    Retrieves the value of a query variable in the WP_Query class.
    to get the current term being queried on a taxonomy page.

    Instead of doing a other query, we’re just fetching the term from the current one. Then we can echoed it out on the front-end.

    <?= get_query_var( 'term' ); ?>