Display Drupal taxonomy terms grouped by vocabulary


/ Published in: XHTML
Save to your folder(s)

These instructions were written for a genesis subtheme but should work for any theme.


Copy this code and paste it in your HTML
  1. Add the following preprocess function to your theme template.php. If you already have this function, then just add the highlighted line.
  2.  
  3. function genesis_custom_preprocess_node(&$vars, $hook) {
  4. $vars['terms_split'] = genesis_custom_split_terms($vars['node']);
  5. }
  6.  
  7. // Add the following function to the bottom of your theme template.php
  8. // Split out taxonomy terms by vocabulary
  9. function genesis_custom_split_terms($node) {
  10. $vocabularies = taxonomy_get_vocabularies();
  11. foreach($vocabularies as $vocabulary) {
  12. $terms = taxonomy_node_get_terms_by_vocabulary($node->nid, $vocabulary->vid);
  13. if ($terms) {
  14. $links = array();
  15. foreach ($terms as $term) {
  16. $links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
  17. }
  18. $items[] = '<span class="label">'. $vocabulary->name .'</span>' . '<span class="sep">:</span> '. implode('<span class="glue">,</span> ', $links);
  19. }
  20. }
  21. return theme('item_list', $items, NULL, 'ul');
  22. }
  23.  
  24. // Finally, in your node.tpl.php add the following line wherever you want the new split terms:
  25.  
  26. <?php print $split_terms ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.