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 have a drop down menu that displays all of the terms for one of my custom taxonomies using get_categories (). It displays fine in alphabetical form. However, what i want to know is how can i have the terms listed in a hierarchial form in a drop down menu, so they appear like this -

Parent Category 1

  • Child One
  • Child Two

Parent Category 2

  • Child One

Here is my code for the drop down menu:

</select> 

<span>By Category: </span>
<select name="category"> 
<option name="all" value="">All Categories</option>
<?php $categories_filter = get_categories( 'taxonomy=tshirt_categories' ); 
    foreach( $categories_filter as $category_option ) { 

    echo '<option name=' . $category_option->slug . ' value="' .  $category_option->slug . '" /> ' . $category_option->name . '</option>' . "\n";
    }
?> 
</select>  

Once again thanks for any advice you guys may have!

Mike

share|improve this question
    
Do you want the categories in a hierarchy in your dropdown <select>, or just printed to a page in a hierarchy? –  GavinR Nov 11 '11 at 1:29
    
If you want them to be listed hierarchical, are you thinking of optgroups within this select where the parent category will be an optgroup label? –  Rutwick Gangurde Nov 11 '11 at 2:51

2 Answers 2

up vote 1 down vote accepted

wp_dropdown_categories has a hierarchical and depth options available.

$args = array(
    'show_option_all'    => 'All Tshirt Categories',
    'orderby'            => 'ID', 
    'order'              => 'ASC',
    'hide_empty'         => 1, 
    'child_of'           => 0,
    'hierarchical'       => 1, 
    'depth'              => 1,
    'taxonomy'           => 'tshirt_categories',
    'hide_if_empty'      => false ); ?>


<h2>By Category</h2>
    <form action="<?php bloginfo('url'); ?>" method="get">
    <div>
    <?php wp_dropdown_categories($args); ?>
    <input type="submit" name="submit" value="view" />
    </div>
    </form>
share|improve this answer

If you are trying to just print the heirarchical categories on the page, you can use this code:

<?php
$args = array(
  'taxonomy'     => 'tshirt_categories'
);

wp_list_categories( $args ); ?>
share|improve this answer
    
Thanks for the tips, but no, i was looking to have them heirarchialy arranged in the dropdown. My bad for leaving it ambiguous. –  MichaelJames Nov 11 '11 at 8:32

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.