How To Separate Drupal Taxonomy Terms Output By Vocabulary

07 Sep · by Tim Kamanin · 2 min read

UPD 06/20/2011: If you're looking for a solution on How To Separate Drupal Taxonomy Terms Output By Vocabulary for Drupal 7 read my new tutorial here.

Happy Autumn everyone! Several days ago I got a question from one of drupal users about „How to output drupal taxonomy terms separated by vocabulary". By default, Drupal outputs all terms despite of their vocabulary in one list, which isn't great at all.

Now I will explain you how to achieve this.

1) Go to your active Drupal theme folder and open (or create and open, if it doesn't exist) template.php file

2) At the bottom of template.php file let's create a function called _yourthemename_ _separate_terms($node_taxonomy) { } Note _yourthemename_ , replace it with your theme name, in my case it is 'garland' so it has name of garland_separate_terms.

3) Pop this function with the following code:

function garland_separate_terms($node_taxonomy) {
  if ($node_taxonomy) { 
  //separating terms by vocabularies 
foreach ($node_taxonomy AS $term) { 
  $links[$term->vid]['taxonomy_term_'. $term->tid] = array(
'title' => $term->name,
'href' => taxonomy_term_path($term),
'attributes' => array(
  'rel' => 'tag', 
  'title' => strip_tags($term->description)
   } //theming terms out 

foreach ($links AS $key => $vid) {
  $terms[$key] = theme_links($vid); 
  return $terms; 

Save your changes.

4) Now go to node.tpl.php file and in the beginning of the file write the following (Note: Change Garland to your theme name):

$terms = garland_separate_terms($node->taxonomy);

5) Now replace old Drupal

<?php print $terms; ?>


Category: <?php print $terms[1]; ?>
<div class="tags">Tags: <?php print $terms[2]; ?>


That's all. Now you have tags and category terms themed and outputed separatelly. Note, that you're passing vocabulary ID to a $terms array to choose a desired vocabulary:

<?php print $terms['here we put a desired vocabulary id']; ?>

It is up to you to style the output and choose a place where to output everything.

If you have some Drupal related questions, ask me and I will write a new tutorial based on your question. Contact me using the form (link).