Show a list (x) of taxonomy terms with node counts

Last modified: November 13, 2009 - 08:00

This simple block snippet displays a list of taxonomy terms in alphabetical order. Each term links to the term's page.

<?php

$vid
= 11;
$num_term = 20;

$query = "SELECT term_data.tid AS tid, name, COUNT( nid ) AS count
FROM term_data
LEFT JOIN term_node ON term_data.tid = term_node.tid
WHERE term_data.vid="
. $vid ."
GROUP BY tid ORDER BY name ASC LIMIT "
. $num_term;

$result = db_query($query);
while (
$term = db_fetch_object($result)) {
 
$items[]= l($term->name, "taxonomy/term/$term->tid") . '(' . $term->count . ')';
}

if(
count($items)) {
  return
theme('item_list',$items);
}

?>

The result will looks like this:

 
 

Drupal is a registered trademark of Dries Buytaert.