I was using wp_dropdown_categories() this week to, well… Create a dropdown menu that links users to the category archives page on a WordPress website, obviously. And I couldn’t figure out how to indent the subcategories, even though that seemed like a very obvious thing you’d want to do.
Google was amazingly useless in this task. Though it almost always is, unless what you are searching for is top 10 list that has almost nothing to do with your query.
Anyway, it’s super easy. The solution:
All you have to do is set the parameter
hierarchical to
true . That’s it!
The WordPress Codex page for this function describes the parameter as such:
Whether to traverse the taxonomy hierarchy. Accepts 0, 1, or their bool equivalents. Default 0.
While you might think that this would simply hide or display the subcategories, it has the bonus of also indenting them built-in. Or at least it did with themes I’ve tried it with.
It did not require any additional tweaking from me: it just worked!
Pretty easy stuff.
Just as an example, here is how I defined my parameters, including this one:
1 2 3 4 5 6 7 8 9 10 11 |
<?php wp_dropdown_categories(array( 'taxonomy' => 'listing_categories', 'selected' => $tag_slug, 'show_option_none' => 'Select category', 'hide_empty' => 1, 'value_field' => 'slug', 'hierarchical' => 1, 'show_count' => 1 )); ?> |
Normally nothing is this easy, but this time it was.