List terms with node count

« Back :: Tagged with Drupal :: Drupal 5.x :: Drupal 6.x :: PHP :: Snippets ::

A PHP snippet that lists terms from given vocabularies with node count.

<?php
/**
 * List terms with node count
 */
$result = db_query("SELECT DISTINCT t.tid, t.name FROM {term_data} t WHERE t.vid IN (4) ORDER BY weight,name", $vid);

while ($t = db_fetch_object($result)) {
  $count = db_result(db_query("SELECT COUNT(nid) FROM {term_node} WHERE tid = %d", $t->tid));
  $list[] = l($t->name, 'taxonomy/term/' . $t->tid) . " ($count)";
}
if ($list) {
  print '<ul><li>'. implode('</li><li>', $list) .'</li></ul>';
}
?>

 

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <blockquote> <blockcode> <pre> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Copyright © 2010 deuxcode.com
Powered by Drupal. Valid XHTML 1.0 Strict and CSS 2.1.