Print a List of Terms in a Vocabulary

Last modified: November 1, 2009 - 10:55

This prints a list on a PHP enabled page in the form of:

  • Term 1 (n)
  • Term 2 (n)
  • Term 3 (n)

n = the number of nodes assigned to that term

<?php
$listlength
= 99; // number of terms to show.

$vocabulary_id = 18; // VID of the vocabulary that you need to index

$result = db_query_range(db_rewrite_sql("SELECT d.tid, d.name, MAX(n.created) AS updated, COUNT(*) AS count FROM {term_data} d INNER JOIN {term_node} USING (tid) INNER JOIN {node} n USING (nid) WHERE d.vid = $vocabulary_id AND n.status = 1 GROUP BY d.tid, d.name ORDER BY name ASC"), 0, $listlength);

$items = array();
while (
$category = db_fetch_object($result)) {
   
$items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid) .'<br />';
  }

print
theme('item_list', $items);
?>

Module Alternative

Alternatively you could simply use the Taxonomy List module which does exactly this with a few more options. It produces a slightly different format, but I guess that can be easily changed with a CSS tweak.

Snippet Alternative

Take a look at this Drupal 6 snippet which displays all toplevel taxonomy terms in a page or a block: http://drupal.org/node/403348

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

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

Take a look at this Drupal 6

Thomas Ash - November 1, 2009 - 10:54

Take a look at this Drupal 6 snippet which displays all toplevel taxonomy terms in a page or a block. may refer to http://drupal.org/node/403348

 
 

Drupal is a registered trademark of Dries Buytaert.