0

I need to build a php foreach that goes through all my terms in my custom taxonomy 'Section'.

I know how to get a foreach of all the terms in 'Section', but what i cant do is build it with levels.

I want the top parent term to be the heading, and its children will be displayed in a list.

Heres an html example of the output i need.

<div class="accordian">
   <div> Term Parent </div>
   <ul>
      <li> Term Child </li>
      <li> Term Child </li>
   </ul>
</div>
<div class="accordian">
   <div> Term Parent </div>
   <ul>
      <li> Term Child </li>
      <li> Term Child </li>
   </ul>
</div>
3

Why not use wp_list_categories( 'taxonomy=section' ) ? It'll do all the nesting & hard work for you!

If you want more control over the HTML output, implement a custom walker.

0

I managed to work on it a little more and built the answer myself. Heres the code i needed.

<?php
    $parent_terms = get_terms('section', 'orderby=name&hide_empty=0&parent=0');
    foreach ( $parent_terms as $parent_term ) { ?>

    <div class="accordian open">
        <div class="accordian-title module-gradient"><?php echo $parent_term->name; ?></div>
        <?php 
            $get_parent = get_term_by('name', $parent_term->name, 'section');
            $parent_id = $get_parent->term_id;
            $child_terms = get_terms('section', 'orderby=name&hide_empty=0&parent='.$parent_id);

            if ( count($child_terms) > 0 ) { ?>

            <div class="accordian-menu">
                <?php foreach ( $child_terms as $child_term ) { ?>
                    <div class="accordian-item">
                       <?php echo $child_term->name; ?>
                    </div>
                <?php } ?>
            </div>
        <?php } ?>
    </div>
<?php } ?>

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.