Display post taxonomies tree

I was able to get all the items of a custom taxonomy for a post, like this:

$args=array('orderby'=>'parent',"fields" => "all");
$term_list = wp_get_post_terms($post->ID, 'tvr_amenity', $args);

My problem is that i would like to show the tree (respecting the parents)

Read More

So i would like to get them ordered by name and parent but i cant find anything related on codex..

any idea how?

Related posts

2 comments

  1. What about:

    $taxName = "tvr_amenity";
    $terms = get_terms($taxName,array('parent' => 0));
    foreach($terms as $term) {
       echo '<a href="'.get_term_link($term->slug,$taxName).'">'.$term->name.'</a>';
       $term_children = get_term_children($term->term_id,$taxName);
       echo '<ul>';
       foreach($term_children as $term_child_id) {
           $term_child = get_term_by('id',$term_child_id,$taxName);
           echo '<li><a href="' . get_term_link( $term_child->name, $taxName ) . '">' . $term_child->name . '</a></li>';
       }
       echo '</ul>';
    }
    
  2. With recursion for all deep.

    function tree() {
    
        $taxName = "custom_tax_name";
        $terms = get_terms($taxName, array('parent' => 0, 'fields' => 'ids'));
        subtree($terms, 0, $taxName);
    }
    
    function subtree($children_ids, $parrent_id, $taxName) {
    
        if ( !empty($children_ids) ){
            echo '<ul>';
                foreach($children_ids as $term_child_id) {
                    $term_child = get_term_by('id', $term_child_id, $taxName);
                    if ( $term_child->parent == $parrent_id) {
                        echo '<li><a href="' . get_term_link( $term_child->term_id, $taxName ) . '">' . $term_child->name . '</a>';
                        $term_children = get_term_children($term_child_id, $taxName);
                        subtree($term_children, $term_child_id, $taxName);
                        echo '</li>';
                    }
                }
            echo '</ul>';
        }
    
    }
    

Comments are closed.