Display taxonomy terms broken out by vocabulary

Last modified: March 27, 2008 - 18:52

By default, taxonomy terms are displayed in the node by the function

<?php
print $terms
?>

This results in one big array of all taxonomy terms associated with that node, no matter what vocabulary. Sometimes it makes sense to break these up, so that the terms are displayed by vocabulary. For example:

  • Topic: foo, bar
  • Tags: bat

To do this, do the following:

Step one

Add the following snippet to your template.php file:

<?php
// split out taxonomy terms by vocabulary
function yourthemename_print_terms($nid) {
    
$vocabularies = taxonomy_get_vocabularies();
    
$output = '<ul>';
     foreach(
$vocabularies as $vocabulary) {
       if (
$vocabularies) {
        
$terms = taxonomy_node_get_terms_by_vocabulary($nid, $vocabulary->vid);
         if (
$terms) {
          
$links = array();
          
$output .= '<li>' . $vocabulary->name . ': ';
           foreach (
$terms as $term) {
            
$links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
           }
          
$output .= implode(', ', $links);
          
$output .= '</li>';
         }
       }
     }
    
$output .= '</ul>';
     return
$output;
}
?>

Note: Be sure to replace 'yourthemename' with the actual name of your theme, or this will not work. [Remember, don't include the php tags if you already have them in your file.]

Step Two

Now add the following to your node.tpl.php file:

<?php
print yourthemename_print_terms($node->nid)
?>

Again, be sure to replace 'yourthemename' with the actual name of your theme, or this will not work. You might want to enclose this snippet in your node template within a div with the appropriate class for your stylesheet.

To move the Taxonomy terms into a block, create a block visible only on node pages and then paste in the following code:

<?php
$nid
= arg(1);
print
yourthemename_print_terms($nid);
?>

Remember to change the input format to PHP.

[Hat tip: styro]

A Modified Version

JuliaKM - November 19, 2007 - 15:44

Thanks for the code. I ended up needing to be able to call each vocabulary individually since I didn't want to list all of my vocabularies on each node. To do this, I came up with the following code:

For template.php

<?php
function mythemename_taxonomy_links($node, $vid) {

//if the current node has taxonomy terms, get them
 
if (count($node->taxonomy)):

   
$tags = array();
     foreach (
$node->taxonomy as $term) {
       if (
$term->vid == $vid):
         
$tags[] = l($term->name, taxonomy_term_path($term));
          endif;
}
    if (
$tags):
//get the vocabulary name and name it $name
       
$vocab = taxonomy_get_vocabulary($vid);
       
$name = $vocab->name;
       
$output .= t("$name") . ": " . implode(' | ', $tags);
    endif;

  endif;

     if (
$output):
       return
$output;
     endif;

}
?>

For node.tpl.php

Note: 1 is the vid for the vocabulary I want to call.

<?php print mythemename_taxonomy_links($node, 1); ?>

Please let me know if you see a better way to do this.

Modified Method

Lynn - March 18, 2008 - 23:42

<?php
function phptemplate_print_terms($nid) {
 
$vocabularies = taxonomy_get_vocabularies();
 
$items = array();
  if (
$vocabularies) {
   foreach(
$vocabularies as $vocabulary) {
     
$terms = taxonomy_node_get_terms_by_vocabulary($nid, $vocabulary->vid);
        if (
$terms) {
         
$links = array();
          foreach (
$terms as $term) {
           
$links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
          }
         
$output = implode(', ', $links);
         
$items[] = $vocabulary->name . ': '. $output;
        }
      }
   }
     return
theme('item_list', $items);
}
?>

Then you need to change the $terms variable to use this function.
Use _phptemplate_variables to set

<?php
$vars
['terms'] = phptemplate_print_terms($vars['nid']);
?>

Use $node object in Drupal 6

lrickard - August 20, 2008 - 13:55

The taxonomy_node_get_terms_by_vocabulary() api function has been changed in Drupal 6. Pass the $node object instead of $nid. (Compare Drupal 5 to Drupal 6 at api.drupal.org.) If you pass $nid in Drupal 6, the function will return an empty array.
http://api.drupal.org/api/function/taxonomy_node_get_terms_by_vocabulary

Thanks to Chill35 in the Drupal Theme Development forum:
http://drupal.org/node/297250

 
 

Drupal is a registered trademark of Dries Buytaert.