1

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!

2 Answers 2

7

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

}
2
  • this is bring up a parse error: Parse error: syntax error, unexpected '['? Nov 10, 2014 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, 2014 at 16:37
0

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');
3
  • 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, 2014 at 16:57
  • If you use your own walker with this method you can get the same flexibility and control.
    – Milo
    Nov 10, 2014 at 19:23
  • @Milo yes, it is all up to the OP what he actually wants to achieve. Must admit, great call Nov 10, 2014 at 19:30

Your Answer

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

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