get taxonomy terms for parent and child

You should have two foreach loops. One for getting parent taxonomy terms, and second for getting child taxonomy terms.

In the second foreach you need to specify the parent taxonomy term ID which is $parent_term->term_id from the first foreach loop.

foreach( get_terms( 'products-category', array( 'hide_empty' => false, 'parent' => 0 ) ) as $parent_term ) {
  // display top level term name
  echo $parent_term->name . '<br>';

  foreach( get_terms( 'products-category', array( 'hide_empty' => false, 'parent' => $parent_term->term_id ) ) as $child_term ) {
    // display name of all childs of the parent term
    echo $child_term->name . '<br>';
  }

}

Leave a Comment