Print taxonomy menu list with multiple hierarchy and display the number of nodes
- Posted By:realworks
- Category:Menus, Categories
- Type:PHP
- Views:703
Prints a fixed taxonomy list with multiple hierarchy as menu items also displaying the number of nodes in each category term. - there, how about that!
Vocabulary
-term cat (5)
--term sub (110)
--term sub (55)
-term cat (14)
-term cat (21)
--term sub (3)
Print in a block or where ever you would like to print a list of taxonomy terms as links.
Snippet:
Login/Register to Comment this Snippet<?php
$vid = 1; // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$pole = array();
$items = array();
$terms = taxonomy_get_tree($vid);
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 * FROM {term_node} WHERE tid=$list[3]");
while($zaznam = db_fetch_array($result)) {
$node = db_result(db_query("SELECT title FROM {node} WHERE nid=$zaznam[nid]"));
$node_link = l($node, "node/$zaznam[nid]");
echo "\n<li>$node_link</li>";
}
echo "\n</ul>";
}
$depth=$list[1];
}
echo "</li>\n</ul>";
?>