I've been searching for documentation or examples on how to modify the output of wp_dropdown_categories so I can add the term ID to each option. Is this possible? If not, my only thought would be to try to do so with jQuery which doesn't seem as reliable or as fast.
Here's my current code:
<div class="location-cats-dropdown">
<form id="location-category-select" class="location-category-select" method="get">
<?php wp_dropdown_categories(
array(
'orderby' => 'menu_order',
'taxonomy' => 'location_category', // Taxonomy slug
'name' => 'location_category', // Taxonomy slug
'value_field' => 'slug',
'show_option_all' => 'Make a selection',
'selected' => km_get_selected_taxonomy_dropdown_term(), // Set which option in the dropdown menu is the currently selected one
)
); ?>
<input type="submit" name="submit" value="Filter" />
</form>
</div><!--attractions-dropdown-->
And here's the current output:
<select name="location_category" id="location_category" class="postform">
<option value="0">Byway Categories</option>
<option class="level-0" value="accommodations">Accommodations</option>
</select>
I'm looking to add the term ID to each option like this example output:
<option class="level-0" id="term-id-48" value="accommodations">Accommodations</option>
Thanks in advance for any info!
I've been searching for documentation or examples on how to modify the output of wp_dropdown_categories so I can add the term ID to each option. Is this possible? If not, my only thought would be to try to do so with jQuery which doesn't seem as reliable or as fast.
Here's my current code:
<div class="location-cats-dropdown">
<form id="location-category-select" class="location-category-select" method="get">
<?php wp_dropdown_categories(
array(
'orderby' => 'menu_order',
'taxonomy' => 'location_category', // Taxonomy slug
'name' => 'location_category', // Taxonomy slug
'value_field' => 'slug',
'show_option_all' => 'Make a selection',
'selected' => km_get_selected_taxonomy_dropdown_term(), // Set which option in the dropdown menu is the currently selected one
)
); ?>
<input type="submit" name="submit" value="Filter" />
</form>
</div><!--attractions-dropdown-->
And here's the current output:
<select name="location_category" id="location_category" class="postform">
<option value="0">Byway Categories</option>
<option class="level-0" value="accommodations">Accommodations</option>
</select>
I'm looking to add the term ID to each option like this example output:
<option class="level-0" id="term-id-48" value="accommodations">Accommodations</option>
Thanks in advance for any info!
Share Improve this question asked Feb 25, 2020 at 16:14 GregGreg 314 bronze badges 3- That function is just a helper function, have you considered cutting out the middle man and just generating the markup yourself? – Tom J Nowell ♦ Commented Feb 25, 2020 at 16:34
- Would you suggest using a get_terms query? – Greg Commented Feb 25, 2020 at 18:21
- get_terms ended up a much better and more customizable solution. – Greg Commented Feb 25, 2020 at 18:47
1 Answer
Reset to default 2get_terms
ended up bring a more customizable solution which is working for my needs. I was able to easily add the term IDs to each option as needed. Hope this helps someone else.
<div class="location-cats-dropdown">
<form id="location-category-select" class="location-category-select" method="get">
<?php $loc_cats = get_terms( array(
'taxonomy' => 'location_category',
'hide_empty' => true,
'orderby' => 'menu_order',
'order' => 'ASC',
) );
if ( $loc_cats ) : ?>
<select name="location_category" id="location_category" class="postform">
<option value="0">Byway Categories</option>
<?php foreach( $loc_cats as $loc_cat ) : ?>
<option value="<?php echo $loc_cat->slug; ?>" id="term-id-<?php echo $loc_cat->term_id; ?>"><?php echo $loc_cat->name; ?></option>
<?php endforeach; ?>
</select>
<?php endif;
wp_reset_postdata(); ?>
<input type="submit" name="submit" value="Filter" />
</form>
</div><!--location-cats-dropdown-->