0

Is there a way that I can list all the terms within a custom taxonomy in an unordered list and wrap all child terms in another unordered list?

For example;

<ul class="menu">

    <li><a href="#">Parent 1</a></li>
    <li>
        <a href="#">Parent 2</a>

        <ul class="sub-menu">

            <li><a href="#">Child of Parent 2</a></li>
            <li><a href="#">Child of Parent 2</a></li>
            <li><a href="#">Child of Parent 2</a></li>

        </ul>

    </li>
    <li><a href="#">Parent 3</a></li>

</ul>

1 Answer 1

1

You can take a look at the Wordpress function wp_list_categories(). If you want to display all the terms within a given custom taxonomy you can for example use:

<?php 
$args = array(
  'taxonomy'     => 'my_custom_taxonomy_slug',
  'orderby'      => 'name',
  'hide_empty'   => 0,
  'title_li'     => '',
  'hierarchical' => 1,
  'walker'       => null,
);
?>
<ul class="menu">
<?php wp_list_categories( $args ); ?>
</ul>

If you need to adjust the html structure, you can use a custom walker that extends either Walker_Category or Walker.

You can read more about it here in the Codex:

http://codex.wordpress.org/Template_Tags/wp_list_categories

2
  • I didn't even need to add a walker, child categories are already contained in a ul! Thanks for steering me in the right direction! Commented Mar 27, 2013 at 20:46
  • you're welcome, good luck with your project.
    – birgire
    Commented Mar 27, 2013 at 20:48

Your Answer

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

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