Skip to main content

Drupal Taxonomy-Based Menu Block

Posted in

I failed with Taxonomy Menu and Menu Block. I couldn't get them to work, the menu appeared in the admin but never showed up in the menu block. I am not sure where the problem was but I wasn't alone with this issue.

I thought perhaps Views could create a view based on taxonomy terms. Failure.

What I did find was some nice code that allowed you to create a PHP code block that did exactly what I wanted.

Simply create a new block, paste in this code, make sure $vid is set to your proper taxonomy, change input type to PHP (enable php filter in modules if it's not showing), save. Add the block wherever you like!

$vid = 1;  // Set the vid to the vocabulary id of the vocabulary you wish to list the terms from
$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));  
    $items[] = l($term->name, "taxonomy/term/$term->tid") . " ($count)";
if ( count($items) ) {  print theme('item_list', $items);}

Also worth mentioning is that my server seemed to slow down to a crawl when I installed those first two modules. It may have been a coincidence, but when I uninstalled them it seemed to get back to regular speed.