I have a function set up that prints the taxonomy term name and slug for every products-category taxonomy term present. This works great, but it's just displaying them alphabetically like so (regardless of if they are a parent / child taxonomy term):

Parent Category 2
Parent Category 1
Child Category 3
Parent Category 3
Child Category 2
Child Category 1 
etc...

Whereas I'm after a structure more like this:

—Parent Category 1
Child Category 1
Child Category 2
Child Category 3

—Parent Category 2
Child Category 1
Child Category 2
Child Category 3

—Parent Category 3
Child Category 1
Child Category 2
Child Category 3

So the children terms of each taxonomy term sit underneath, so you know what parent they belong to. My markup is as follows:

<?php
$args = array(
    'hide_empty' => false
);  
 $terms = get_terms("products-category");
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     foreach ( $terms as $term ) { ?>
      <option value=".<?php echo $term->slug; ?>" data-hook="<?php echo $term->slug; ?>"><?php echo $term->name; ?></option>
    <?php }
 } ?>   

Any suggestions on how to achieve this would be greatly appreciated!

up vote 7 down vote accepted

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

}
  • this is bring up a parse error: Parse error: syntax error, unexpected '['? – user1374796 Nov 10 '14 at 16:35
  • This because of your PHP version. For this syntax you should have PHP 5.4+ I believe. Check my updated answer! [] equals array() btw. – SLH Nov 10 '14 at 16:37

You can simply make use wp_list_categories to display your list. It is extremely versatile and can easily be modified to suite your specific needs. Although the name suggest that it is just for the purpose of the build in taxonomy category, it is not. It can be used for any taxonomy

You'll have to work through examples etc and modify as needed. Here is a simple example that you can use to display your list

wp_list_categories('taxonomy=products-category');
  • By using my method he has more flexibility and control of how his taxonomy terms are displayed. But your answer is OK as well. – SLH Nov 10 '14 at 16:57
  • If you use your own walker with this method you can get the same flexibility and control. – Milo Nov 10 '14 at 19:23
  • @Milo yes, it is all up to the OP what he actually wants to achieve. Must admit, great call – Pieter Goosen Nov 10 '14 at 19:30

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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