【发布时间】:2016-01-08 17:40:26
【问题描述】:
我正在使用 Wordpress 函数添加一个下拉列表,其中填充了自定义分类条目以过滤帖子。下拉菜单已正确创建和填充,但我无法设置默认值(我希望这是“所有批次”)或在用户选择过滤选项时设置当前选择。
我尝试如下动态添加 selected="selected",但每次下拉值默认为选择菜单中的最终选项。 selected="selected" 被添加到正确的选项中 - 我可以在 firebug 中看到它。
如何将默认选项设置为“所有手数”以及如何显示当前选择的选项?
function custom_taxonomy_dropdown( $taxonomy ) {
$terms = get_terms( $taxonomy );
if ( $terms ) {
echo '<form id="sale_selector" method="POST">
<select id="sale_selection" name="' . $taxonomy . '" class="postform">
<option value="">All Lots</option>';
foreach ( $terms as $term ) {
if ( $_POST["online_lot_category"] == $term->slug ){
$selected_option = 'selected';
} else {
$selected_option = '';
}
echo '<option selected="'.$selected_option.'" value="' . $term->slug . '">' . $term->name . '</option>';
}
echo '</select>
<input class="filter_button" type="submit" value="OK">
</form>';
}
}
【问题讨论】:
标签: html wordpress drop-down-menu