I have a custom hierarchical taxonomy. I don't know how many nested terms will be in each term.

What I want to do, is to list all taxonomy terms hierarchically no matter how deep each term is nested.

I already know how to do this for each level if I know the depth, but how can I rewrite my code so it will handle unlimited amount of nested terms?

foreach( get_terms( 'tax', array( 'parent' => 0 ) ) as $term ) {
  echo $term->name;
  foreach( get_terms( 'tax', array( 'parent' => $term->term_id ) ) as $child_term ) {
    echo $child_term->name;
    // of course I can do more foreach loops here for every lvl...

1 Answer 1


You could use wp_list_categories, it can be used for any taxonomy, not only categories:

$args = array(
    'taxonomy' => 'tax'
wp_list_categories( $args );

See the list of arguments in the codex.

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.