1

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)

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

any idea how?

2 Answers 2

3

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>';
}
3
  • Thats exactly what I was looking for! I love you mate! Commented Aug 19, 2013 at 18:49
  • How we can show all subchildren like this? Commented Jan 30, 2017 at 14:46
  • @Alexandros All subchildren of what?
    – Angelique
    Commented Jan 30, 2017 at 16:29
1

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>';
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.