Display Top Level Taxonomy Terms

This PHP snippet displays all top-level taxonomy terms in a page or a block.

For each term, the count of sub-terms is displayed.

The code is using the taxonomy API, so it's compatible with taxonomy_redirect (verified). It should also be compatible with other add-on modules which change the term path.

<?php
$vid
= 3;
$terms = taxonomy_get_children(0, $vid);
if (
count($terms)) {
  echo
'<ul>';
  foreach (
$terms as $term){
    echo
"<li>" . l($term->name, taxonomy_term_path ($term)) ;
   
$children = taxonomy_get_children($term->tid, $vid);
   
$cnt = count($children);
    if (
$cnt) echo " ($cnt)";
    echo
"</li>";
  }
  echo
'</ul>';
}
?>

Contact Me

Feel free to contact me.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.