Question

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!

Was it helpful?

Solution

get_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-->
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top