My best solution! (适用于任何分类法,包括“类别”)
$your_taxonomy=\'category\';
function my_Categ_tree($TermName=\'\', $termID, $separator=\'\', $parent_shown=true ){
$args = \'hierarchical=1&taxonomy=\'.$TermName.\'&hide_empty=0&orderby=id&parent=\';
if ($parent_shown) {$term=get_term($termID , $TermName); $output=$separator.$term->name.\'(\'.$term->term_id.\')<br/>\'; $parent_shown=false;}
$separator .= \'-\';
$terms = get_terms($TermName, $args . $termID);
if(count($terms)>0){
foreach ($terms as $term) {
//$selected = ($cat->term_id=="22") ? " selected": "";
//$output .= \'<option value="\'.$category->term_id.\'" \'.$selected .\'>\'.$separator.$category->cat_name.\'</option>\';
$output .= $separator.$term->name.\'(\'.$term->term_id.\')<br/>\';
$output .= my_Categ_tree($TermName, $term->term_id, $separator, $parent_shown);
}
}
return $output;
}
Then you can output:1)目标类别(分类)树,使用特定ID
echo my_Categ_tree($your_taxonomy, 0 );
2)所有类别/分类
foreach (get_terms($your_taxonomy, array(\'hide_empty\'=>0, \'parent\'=>0)) as $each) {
echo my_Categ_tree($each->taxonomy,$each->term_id);
}