Expandable parent terms with child terms as links taxonomy menu
- Posted By:realworks
- Category:Menus, Categories
- Version:5.x
- Type:PHP
- Views:2443
This Drupal snippet prints 1 vocabulary's parent terms as expandable menus and the child terms as links to SEO friendly url's.
Parent term 1 (expandable)
- child term 1 (link)
- child term 1 (link)
Parent term 2 (expandable)
- child term 3 (link)
Our thanks to www.computerminds.co.uk for the modifications to this.
Print into a block or where ever you would like the category list/menu to appear.
Snippet:
<?php
// accordion taxonomy menu
// by Chris Herberte
// ** don't forget to change $vocabulary
//
$vocabulary = 1;
//drupal_add_js('misc/collapse.js');
$lastdepth = 1;
$topoflist = 1;
$nid = 0;
if (arg(0) == 'node' && is_numeric(arg(1)) && ! arg(2)) {
$nid = arg(1);
$terms = taxonomy_node_get_terms($nid);
rsort($terms);
$tid = $terms[0]->tid;
} elseif (arg(0) == 'taxonomy' && arg(1) == 'term') {
$terms = preg_split('/[+ ,]/', arg(2));
$tid = $terms[0];
}
$parents = taxonomy_get_parents($tid);
foreach ($parents as $parent) {
$i++;
}
$tree = taxonomy_get_tree($vocabulary);
foreach ($tree as $key => $term) {
if ( $term->depth == 0 ) {
$title = $term->name;
if ($topoflist == 0) {
print '</ul></li></ul>'."\n";
}
if ($tree[$key+1]->depth==1){
if ($term->tid == $parent->tid) {
print '<ul>'."\n".
'<li class="title active" onClick="showHide(event)">'.$title.
"\n".'<ul class="active">'."\n";
} else {
print '<ul>'."\n".
'<li class="title" onClick="showHide(event)">'.$title.
"\n".'<ul class="menu">'."\n";
}
}
else {
print '<ul>'."\n".
'<li class="title">'.l($title,'taxonomy/term/'.$term->tid).
"\n".'<ul class="menu">'."\n";
}
$topoflist = 0;
}
if ($term->depth > 0) {
if ($term->tid == $tid) {
print '<li id="darker">' . l($term->name, "taxonomy/term/$term->tid") . '</li>' . "\n";
} else {
print '<li class="leaf notdarker">' . l($term->name, "taxonomy/term/$term->tid") . '</li>' . "\n";
}
}
$lastdepth = $term->depth;
}
print '</ul></li></ul>';
?>
I tried to post this into a block and try it, but all i get is a block with code in it. I created a input format without any filters and used that input format for the block. I thought maybe fck was messing it up, so I turned fck off. Still no luck. Any insights/
This should work fine with php as the block filter. Not sure what you mean by fck?
Thank you for this code ... as it helped me to print what i wanted