我找到了以下函数,可以将自定义分类下拉列表添加到搜索表单中。它可以很好地进行搜索,但不会将“selected”类添加到搜索的术语中。这是我的分类代码,名为grades
:
function get_terms_grades_dropdown($taxonomies, $args)
{
global $search_values;
$myterms = get_terms($taxonomies, $args);
$output = "";
foreach ($myterms as $term) {
$root_url = get_bloginfo(\'url\');
$term_taxonomy = $term->taxonomy;
$term_slug = $term->slug;
$term_name = $term->name;
$value = $term->term_id;
if ($search_values[\'grades\'] == $term->slug) {
$selected = "selected=\'selected\'";
} else {
$selected = \'\';
}
$output .= "<option value=\'" . $value . "\' " . $selected . ">" . $term_name . "</option>";
}
return $output;
}
然后我使用如下函数:
<select name="grade" id="grade_search">
<option value="" selected="selected">Grade Level</option>
<?php
$taxonomies = array(\'grades\');
$args = array(\'orderby\'=>\'count\',\'hide_empty\'=>true);
echo get_terms_grades_dropdown($taxonomies, $args);
?>
</select>
问题在于这一行:
if ($search_values[\'grades\'] == $term->slug) {
我需要
$search_values[\'grades\']
等于搜索的项。
非常感谢。