Output full taxonomy hierarchy

<h2><?php echo taxonomy_vocabulary_machine_name_load('tags')->name; ?></h2>
<?php
function taxonomy_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 = taxonomy_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
}
}

return $return;
}
function output_taxonomy_nested_tree($tree) {
if (count($tree)) {
$output = '<ul class="taxonomy-tree">';
foreach ($tree as $term) {
$output .= '<li class="taxonomy-term">';
$output .= '<a href="' . drupal_get_path_alias('taxonomy/term/'.$term->tid) . '">';
$output .= $term->name;
$output .= '</a>';
if ($term->children) {
$output .= output_taxonomy_nested_tree($term->children);
}
$output .= '</li>';
}
$output .= '</ul>';
}
return $output;
}
$tree= taxonomy_get_nested_tree(1,10);
$output=output_taxonomy_nested_tree($tree);
echo $output;

?>

Tags: