我有一个下拉列表,显示了具有多个级别(子级)的自定义分类法。下面是它的代码。有人能解释一下我如何限制孩子们观看吗?例如,目前它显示:
父->子->子对象的子对象
我只想让它显示:
父->子级
谢谢!:)
function my_dropdown_categories( $taxonomy, $current_selected = \'\', $include = null ) {
// Get all terms of the chosen taxonomy
$terms = get_terms($taxonomy, array(\'orderby\' => \'name\'));
// our content variable
$list_of_terms = \'<select id="location" data-placeholder="Choose your area..." class="selectboxSingle" name="location">\';
if ( ! is_wp_error( $terms ) ) foreach($terms as $term){
// If include array set, exclude unless in array.
if ( is_array( $include ) && ! in_array( $term->slug, $include ) ) continue;
$select = ($current_selected == $term->slug) ? "selected" : ""; // Note: ==
if ($term->parent == 0 ) {
// get children of current parent.
$tchildren = get_term_children($term->term_id, $taxonomy);
$children = array();
foreach ($tchildren as $child) {
$cterm = get_term_by( \'id\', $child, $taxonomy );
// If include array set, exclude unless in array.
if ( is_array( $include ) && ! in_array( $cterm->slug, $include ) ) continue;
$children[$cterm->name] = $cterm;
}
ksort($children);
// OPTGROUP FOR PARENTS
if (count($children) > 0 ) {
// $list_of_terms .= \'<optgroup label="\'. $term->name .\'">\';
if ($term->count > 0)
$list_of_terms .= \'<option class ="group-result" value="\'.$term->slug.\'" \'.$select.\'>\' . $term->name .\' </option>\';
} else
$list_of_terms .= \'<option value="\'.$term->slug.\'" \'.$select.\'>\'. $term->name .\' </option>\';
//$i++;
// now the CHILDREN.
foreach($children as $child) {
// $select = ($current_selected == $child->slug) ? "selected" : ""; // Note: child, not cterm
$list_of_terms .= \'<option value="\'.$child->slug.\'" \'.$select.\'>\'. $child->name.\' </option>\';
} //end foreach
if (count($children) > 0 ) {
$list_of_terms .= "</optgroup>";
}
}
}
$list_of_terms .= \'</select>\';
return $list_of_terms;
}