Nested category links with node counts

Last modified: January 8, 2009 - 21:41

Thought I would add this snippet here. Credits go to nevets from the original at http://drupal.org/node/121662#comment-250362

<?php
if ( ! function_exists('private_get_children') ) {
    function
private_get_children($vid, $parent = 0) {
       
$items = array();
       
$terms = taxonomy_get_tree($vid, $parent, -1, 1);
        foreach (
$terms as $term ) {
           
$count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $term->tid));
           
$items[] =  l($term->name, "taxonomy/term/$term->tid") . " ($count)" . private_get_children($vid, $term->tid);
        }
        if (
count($items) ) {
            return
theme('item_list', $items);
        }
    }
}

$vid = 6// Set the vid to the vocabulary id of the vocabulary you wish to list the terms from

print private_get_children($vid);
?>

 
 

Drupal is a registered trademark of Dries Buytaert.