I have a function that queries the children of each taxonomy terms, I'd also like to query the children of these children terms too, my markup is as follows:

$terms = get_terms("wpsc_product_category");
if ( !empty( $terms ) && !is_wp_error( $terms ) ){
    foreach( get_terms( 'wpsc_product_category', array( 'hide_empty' => false, 'parent' => 0 ) ) as $parent_term ) { ?>

        <div id="sub-header-menu-<?php echo $parent_term->slug; ?>" class="sub-header-menu-content">
            <ul class="sub-header-menu">
            <?php foreach( get_terms( 'wpsc_product_category', array( 'hide_empty' => false, 'parent' => $parent_term->term_id ) ) as $child_term ) { ?>
            <li data-hook="<?php echo $child_term->slug; ?>">
                <a href="<?php echo home_url(); ?>/products/<?php echo $child_term->slug; ?>"><?php echo $child_term->name; ?></a>
            <?php } ?>

    <?php }

} ?>

Is this possible? Any suggestions would be greatly appreciated!


Try to use child_of at your second foreach, see the documentation.


(integer) Get all descendents of this term. Default is 0. Note: the difference between child_of and parent is that where parent only gets direct children of the parent term (ie: 1 level down), child_of gets all descendants (as many levels as are available)

So in your case your second foreach will look like this:

<?php foreach( get_terms( 'wpsc_product_category', array( 'hide_empty' => false, 'child_of' => $parent_term->term_id ) ) as $child_term ) { ?>

You can use the returning value parent in a conditional statement to detect on what "level" the term is. Of course you can do this also with one foreach.

  • thanks for your answer, very very close here. Although this foreach is display all terms, parent, children and grand-children. I was hoping to displaying the grand-children as a sub-menu within the relevant child term if possible? – user1374796 Feb 4 '15 at 11:46
  • @user1374796 Have a look at my latest edit, use the returning value parent like this. – Fleuv Feb 4 '15 at 12:58

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.