Populating dropdown menu with hierarchical taxonomies

Im currently populating my <select> elements (I’ve got plenty of them) with nested foreach loops, get_terms() and &nbsp;&nbsp;&nbsp;&nbsp; to create padding for each nested child element.

Each <select> code is about 50 lines long and is looking very dirty – I always have to take few minutes to understand what’s going on before changing anything. Also, if taxonomy is selected, it includes padding to selected element (if dropdown is closed) which would require unnessesary jQuery to remove padding from selected element.

Is there a WordPress function I could use to populate my dropdowns with hierarchical taxonomies? ( maximum depth is 3 )

1 Answer
1

Sounds like you want wp_dropdown_categories() to me:

$tax_args = array(
  'taxonomy'     => 'location',
  'orderby'      => 'name',
  'show_count'   => 1,
  'hierarchical' => 1,
);
wp_dropdown_categories($tax_args);

Leave a Comment