我用这个:
$tree = extranet_get_nested_tree((int)$vid, null, $tid);
有了这个:
function extranet_get_nested_tree($terms = array(), $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0) {
if (is_int($terms)) {
$terms = taxonomy_get_tree($terms);
}
foreach($terms as $term) {
foreach($term->parents as $term_parent) {
if ($term_parent == $parent) {
$return[$term->tid] = $term;
}
else {
$parents_index[$term_parent][$term->tid] = $term;
}
}
}
foreach($return as &$term) {
if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) {
$term->children = extranet_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
}
}
return $return;
}
并打印出来:
function extranet_output_nested_tree($tree) {
if (count($tree)) {
$output = '<ul class="nested-taxonomy-tree">';
foreach ($tree as $term) {
$output .= '<li class="taxonomy-term">';
$output .= t($term->name); //, taxonomy_term_path($term));
if ($term->children) {
$output .= extranet_output_nested_tree( $term->children);
}
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
享受!