Navigating through Taxonomy in to nodes (custom menu block)
nimzie - August 20, 2008 - 13:57
I'm trying to create a menu block. This menu block would know which vid it's dealing with and would list the terms of the vocabulary. From there, each term would also show a link to the nodes which are categorized under that link. In other words, I'm trying to create full taxonomy navigation right in to the node.
I've tried taxonomy_menu and taxonomy_navigation and can't seem to get either to do what I want.
ideally:
term
----content
term
----content
----content
term
----content
All would be expandible and collapsible just like a normal Drupal Menu.
Has anyone got tips on how I'd do best to get this done?
Thank you!
Adam

I've found this, but the one
I've found this, but the one thing it's missing is the ability for me to have the sub items collapsing and expanding as I would expect the nav to...
Can anyone assist based on this piece?
<?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>";
?>
Subscribing, greetings, Marti
Subscribing,
greetings,
Martijn
This is slick and it works
This is slick and it works just as I would have expected a Taxonomy Nav to work. Thanks corey_p again for your help!
<?php
// $vid = ID of the vocabulary you want to print
// $expand_all = should every menu item be expanded by default?
//$list_all = should taxonomy terms be printed, regardless of whether or not
//are there child terms?
$vid = 1;
$expand_all = false;
$list_all = false;
$node_tids = array();
if (arg(0) == 'node' && is_numeric(arg(1))){
$node_tids = taxonomy_node_get_terms(arg(1));
}
elseif (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))){
$node_tids[arg(2)] = '';
}
$node_tids = array_keys($node_tids);
$taxonomy = taxonomy_get_tree($vid);
$query = 'SELECT DISTINCT tn.nid, n.title FROM {term_node} tn LEFT JOIN {node} n ON (tn.nid = n.nid) WHERE tn.tid = %d AND n.status = 1 ORDER BY n.title';
print '<ul class="menu">';
foreach ($taxonomy as $t){
$resource = db_query($query, $t->tid);
$children = db_num_rows($resource);
$expanded = $children && (in_array($t->tid, $node_tids) || $expand_all);
$class = $children ? ($expand_all ? 'expanded' : ($expanded ? 'expanded' : 'collapsed')) : 'leaf';
if ($list_all || $expanded || ($children && !$expanded)){
print "<li class=\"$class\">" . l($t->name, 'taxonomy/term/' . $t->tid);
if ($expanded) {
print '<ul>';
while ($r = db_fetch_array($resource)) {
print '<li class="leaf">' . l($r['title'], 'node/'.$r['nid']) . '</li>';
}
print '</ul>';
}
print '</li>';
}
}
print '</ul>';
drupal_add_js(); // make sure that jQuery is loaded on the page
?>
Thank you so much
Thank you for posting this nimzie. I have been searching for such a code for two days and was already losing hope. Thank you so much for finding this!
Is this the fix I need?
I thought you could do this out of the box with Taxonomy_Menu...
Is that why I'm having troubles? The menu doesn't always load all the nodes, and if I play with the settings, the nodes fall off.
Please check out my issue. Will the code posted above fix this?
Sorry to reopen this*works now*
Hi i added this code to my site that is Drupal 6 core, and i get blank pages after puting the block visible....
I issued smth similar trying to get this menu view by term and nodes with taxonomy treemenu because my PHP version (5.1.6)...
i managed displaying the term->nodes but i want to be collapsible.
I first suppose that the issue is the drupal functions (core 5) not the PHP (hope)...
have anyone tried this code for Drupal 6...
Thanks
acikamk
---------
I hacked it... it was db_nmb_rows that is Drupal 6 db_affected rows... works perfectly thanks......
acikamk