0

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

  • 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
1

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>
0

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 ); ?>
  • Thanks for the tips, but no, i was looking to have them heirarchialy arranged in the dropdown. My bad for leaving it ambiguous. – user10106 Nov 11 '11 at 8:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy