Drupal: How To Show List Of Terms In Alphabetical Order
In this article, I would tell you how Drupal allows to show a list of a certain vocabulary taxonomy terms in alphabetical order. We will also get know of how to show node list for every term.
Use the following code show the list:
<?php
$vid = 3; // vocabulary number
$pole = array();
$items = array();
$terms = taxonomy_get_tree($vid);
usort($terms,create_function(‘$a,$b’,’return strcasecmp ($a->name,$b->name);’));
foreach ( $terms as $term ) {
$count = db_result(db_query(“SELECT COUNT(nid) FROM {term_node} WHERE tid = %d”, $term->tid));
$pole[]=Array (l($term->name, “taxonomy/term/$term->tid”) . ” ($count)”, $term->depth, $count, $term->tid) ;
}
$depth =-1;
foreach ($pole as $list) {
if ($list[1] > $depth) echo “\n<ul>”;
if ($list[1] < $depth) echo “\n</li>\n</ul>\n</li>”;
if ($list[1] == $depth) echo “</li>”;
$poc++;
echo “\n<li>$list[0]”;
if ($list[2]>0) {
echo “\n<ul>”;
$result = db_query(“SELECT n.title, n.nid FROM {term_node} t INNER JOIN {node} n ON t.nid=n.nid WHERE t.tid=$list[3] ORDER BY n.title ASC”);
while($zaznam = db_fetch_array($result)) {
$node_link = l($zaznam[title], “node/$zaznam[nid]”);
echo “\n<li>$node_link</li>”;
}
echo “\n</ul>”;
}
$depth=$list[1];
}
echo “</li>\n</ul>”;
?>
Good luck!