Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
    
Post your foreach loop, so we can alter it. –  kaiser Jun 17 '11 at 15:25
    
possible duplicate of How to show a hierarchical terms list ? –  t31os Jun 19 '11 at 10:05
add comment

2 Answers

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.

share|improve this answer
add comment
up vote 0 down vote accepted

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 } ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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