Display the Terms from a Custom Taxonomy Assigned to a Post(inside the loop) in Hierarchial Order

This link tells me how to display categories assigned to a post.

How would I modify this code to display the terms from a custom taxonomy assigned to a post in hierarchical order?

Related posts

1 comment

  1. It depends of what you are trying to do exactly. But if you want the same functionality of the question you linked, you can use the same function but passing your custom taxonomy to the function. Here all the arguments you can pass to wp_list_categories() and is defulat values:

    <?php $args = array(
         'show_option_all'    => '',
             //Possible values of orderby: ID, name, slug, count, term_group 
         'orderby'            => 'name',
         'order'              => 'ASC',
         'style'              => 'list',
         'show_count'         => 0,
         'hide_empty'         => 1,
         'use_desc_for_title' => 1,
         'child_of'           => 0,
         'feed'               => '',
         'feed_type'          => '',
         'feed_image'         => '',
         'exclude'            => '',
         'exclude_tree'       => '',
         'include'            => '',
         'hierarchical'       => 1,
         'title_li'           => __( 'Categories' ),
         'show_option_none'   => __('No categories'),
         'number'             => null,
         'echo'               => 1,
         'depth'              => 0,
         'current_category'   => 0,
         'pad_counts'         => 0,
              //Change this with your custom taxonomy
         'taxonomy'           => 'your_custom_taxonomy',
          'walker'             => null
    );
    wp_list_categories($args);
    ?>
    

    The above code will generate a list in hierarchical order with all the terms from the specified taxonomy, not only the associated to the current post. But we can limit the included terms using the inlcude argument:

    <?php
    global $post;
    $taxonomy = 'your_taxonomy';
    
    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    
    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {
    
        $term_ids = implode( ',' , $post_terms );
        $args = array(
                    'taxonomy'    => $taxonomy,
                    'include'     => $term_ids,
                    //Add more arguments if you need them with a different value from default
                );
        $terms = wp_list_categories($args);
    
        // display post categories
        echo  $terms;
    }
    ?>
    

Comments are closed.