这里有两个问题,一个是性能问题,另一个是大问题,如果启用调试,将返回一堆错误
这里的性能问题是您第一次获得条款。看来你在这里的目标是只获得顶级条款,而你却获得了所有条款。我想补充一下\'parent\' => 0
只获取顶级术语的参数。然后,您可以在检查的地方删除条件if ( $term->parent == 0 )
因为所有条款都是顶级条款0
作为术语父级,因此此条件将始终返回true
你的大问题是这个代码,我不理解你的逻辑
$uchildren =get_terms( $taxonomy, array(\'hide_empty\' => 0, \'parent\' => $term->term_id ));
$children = array();
foreach ($uchildren 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);
您的直系子女是正确的,因此您的
$uchildren
很好。从那以后一切都乱了套
为什么要使用get_term_by()
在这里$child
已保存您使用的术语属性get_terms()
检索术语。
你不仅仅是在使用get_term_by()
这里是错误的,但您的参数是无效的,并且您正在向它传递一个完整的对象。您应该咨询法典以正确使用此功能。总之,应该从代码中删除该部分
那部分应该是这样的
$uchildren =get_terms( $taxonomy, array(\'hide_empty\' => 0, \'parent\' => $term->term_id ));
$children = array();
foreach ($uchildren as $child) {
if ( is_array( $include ) && ! in_array( $child->slug, $include ) ) continue;
$children[$child->name] = $child;
}
ksort($children);
编辑这里是一个完整的工作代码,修复了几个bug。请查看我在代码中的评论以获取更新
function my_dropdown_categories( $taxonomy, $current_selected = \'\', $include = null )
{
/*
* Declare your variable first. Without this, your code has a bug if no terms are found
*/
$list_of_terms = \'\';
/**
* Get all parent terms. Note we use \'parent\' => 0 to only get top level terms
*
* @see get_terms
* @link http://codex.wordpress.org/Function_Reference/get_terms
*/
$terms = get_terms( $taxonomy, array( \'orderby\' => \'name\', \'parent\' => 0 ) );
/*
* Use curlies here to enclose your statement. Also, check whether or not you have terms
*/
if ( $terms && ! is_wp_error( $terms ) ) {
/*
* Moved this section inside your if statement. We don\'t want to display anything on empty terms
*/
$list_of_terms .= \'<select id="location" class="selectboxSingle" name="location">\';
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: ==
/*
* Use the parent term term id as parent to get direct children of the term
* Use child_of if you need to get all descendants of a term
*/
$uchildren = get_terms( $taxonomy, array(\'hide_empty\' => 0, \'parent\' => $term->term_id ));
$children = array();
foreach ($uchildren as $child) {
// If include array set, exclude unless in array.
if ( is_array( $include ) && ! in_array( $child->slug, $include ) ) continue;
$children[$child->name] = $child;
}
ksort($children);
// PARENT TERM
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>\';
};
// now the CHILDREN.
foreach($children as $child) {
$select = ($current_selected == $child->slug) ? "selected" : ""; // Note: child, not cterm
$list_of_terms .= \'<option class="result-sub" value="\'.$child->slug.\'" \'.$select.\'>\'. $child->name.\' </option>\';
} //end foreach
}
/*
* Moved this section inside your if statement. We don\'t want to display anything on empty terms
*/
$list_of_terms .= \'</select>\';
}
return $list_of_terms;
}