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).


Required for comment verification


Thank you for this tutorial, I tried many others but this did the job!

Reply · 7 years, 3 months ago

Thanks for your tutorial. I successfully could separate and display several taxonomy vocabularies. However only the thing is that the hierarchical taxonomy is not displayed. If you please would you tell me which part of code to modify ? Thanks

Reply · 7 years, 3 months ago
Tejas P Mehta

Thanks. It worked like a charm. Searching for this from past few days and so relived that it is done. Tip for Fusion theme: For Fusion themes just copy node.tpl.php file from fusion folder to your theme folder.

Reply · 7 years, 3 months ago

Great function! Thanks a lot! However, as it appears now: Category: Term Is it possible to make it in one line, like this? Category: Term

Reply · 7 years, 3 months ago

I found solution. To place Category: Tag in one line, in template.php, just before "return $terms;" paste this: $terms = preg_replace('~<(ul|li)[^>]+>~i','<$1>',$terms); $terms = str_replace('

','',$terms); $terms = str_replace('

','',$terms); $terms = str_replace('

  • ','',$terms); $terms = str_replace(' ','',$terms);
Reply · 7 years, 3 months ago

I want to display the terms, but not as links. How would I do this? I think it's $term->name that I want to use? How does that fit into the example?

Reply · 7 years, 3 months ago

on the same line :How do I achieve this.I have names of players in the vocabulary.Now when I put content in my story, and the content has one of the players name[which is the name in the vocabulary too]-then it should automatically show it as a hyperlink which takes you to that players profile page.

Reply · 7 years, 3 months ago

I too would love to know if there is a way to do in Drupal 7. It's one of the last things stumping me.

Reply · 7 years, 3 months ago

very nice tutorial. saved a lot time. Thank a lot man!

Reply · 7 years, 3 months ago

Hi Tim, I'm currently setting up a new D7 installation with the Bartik theme I would love to seperate the taxonomy as this code did in D6. I can't get this code getting working in D7. I noticed Bartik has a function to implement fields: template.php/function/bartik_field__taxonomy_term_reference/7 Any chance you can help me?

Reply · 7 years, 3 months ago

Any updates on this code for D7?

Reply · 7 years, 3 months ago

Thanks for writing this - I'm trying to do the same thing, so finding this example has been a great find.

Reply · 7 years, 3 months ago

Thanks, great snippet

Reply · 7 years, 3 months ago
Rob Davis

This is exactly what I am looking for but I am getting an error: warning: Invalid argument supplied for foreach() in ../sites/all/themes/corolla/template.php on line 154. warning: Invalid argument supplied for foreach() in ../sites/all/themes/corolla/template.php on line 154. warning: Invalid argument supplied for foreach() in ..sites/all/themes/corolla/template.php on line 154.

Reply · 7 years, 3 months ago

Hi, great snippet thanks very much? Any idea of how to modify it to display a comma separated list instead of a UL?

Reply · 7 years, 3 months ago

Nice tutorial, very useful..

Reply · 7 years, 3 months ago

Thanks. I'll give this a shot.

Reply · 7 years, 3 months ago