0

I have a custom hierarchical taxonomy named custom_taxonomy. For example:

Term 1

  • term 1_1
  • term 1_2
  • term 1_3

Term 2

  • term 2_1
  • term 2_2
  • term 2_3

I want to display them on the post page as like in example above: name of parent taxonomy and all its childs, then another parent taxonomy and all childs of this one.

Could you please advice the best way to do this. I thought to take only parents taxonomies via get_terms and then through the loop take childs elements by get_terms or get_term_children. Only one thing, I don't know how to display only terms related to current post in this case.

  • Can you clarify what is a term and what is a taxonomy in your example. Remember, a Taxonomy is the name of the classification. So "Category" and "Tag" are taxonomies, while "Cooking" might be a term in the Category taxonomy, while "Baking" might be a child-term of "Cooking". So there's no such thing as a child taxonomy. – Jacob Peattie Sep 14 '17 at 13:42
  • Taxonomy - name_proc. Terms: Cooking - parent, Baking - child term – Влад Sep 14 '17 at 13:48
1

This has been already answered here

Both the loops needs to exist and you can display it however you like.

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

}
  • This is what I did. But in this case will be displayed all terms, instead of display terms of current post – Влад Sep 14 '17 at 14:05
  • This was not visible in the question. But here you go. Use $the_post_cat= get_the_terms( $post->ID, 'category' ); Use Your category you want and after that you can display it. – Drupalizeme Sep 14 '17 at 14:15
  • I've found out. Just need to use 'wp_get_post_terms' instead of 'get_terms' – Влад Sep 14 '17 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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