我一直在搜索关于如何修改wp\\u dropdown\\u categories的输出的文档或示例,以便我可以将术语ID添加到每个选项中。这可能吗?如果不是这样,我唯一的想法就是尝试使用jQuery,它看起来不那么可靠,也不那么快。
这是我当前的代码:
<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-->
这是电流输出:
<select name="location_category" id="location_category" class="postform">
<option value="0">Byway Categories</option>
<option class="level-0" value="accommodations">Accommodations</option>
</select>
我希望将术语ID添加到每个选项中,如以下示例输出:
<option class="level-0" id="term-id-48" value="accommodations">Accommodations</option>
提前感谢您提供的任何信息!
最合适的回答,由SO网友:Greg 整理而成
get_terms
最终带来了一个更可定制的解决方案,它满足了我的需求。我能够根据需要轻松地将术语ID添加到每个选项中。希望这对其他人有帮助。
<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-->