How To Separate Drupal Taxonomy Terms Output By Vocabulary

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. On the output we want to get something like this:

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; ?>
with
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).

Comments

Submitted by Anonymous on Mon, 09/13/2010 - 18:37

Thanks. I'll give this a shot.

Submitted by Anonymous on Mon, 09/13/2010 - 20:44

Nice tutorial, very useful..

Submitted by Rob Davis on Sat, 09/18/2010 - 00:58

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.

Submitted by Tim on Sat, 09/18/2010 - 13:27

Hello!

Let me know what piece of code you have on line 154. I guess, that you passed wrong argument to a fucntion, make sure that you wrote like this in the function call: $terms = corolla_separate_terms($node->taxonomy); (note $node->taxonomy here).

Submitted by Anonymous on Sun, 09/19/2010 - 06:55

Hey Tim,

Thanks for replying.

My problem was that a few of the nodes on my front page didn't have any vocabulary terms/tags. (I added the taxonomy vocabularies after I had posted them).

So I would suggest putting an if statement around your foreach loop that checked to make sure that terms existed before running the code. Or just making sure that each node has taxonomy terms entered.

Between you and me, don't you wish that Drupal just passed a variable for each vocabulary? Something like - print $term->vocabularyname - would be so much simpler.

Thanks for the sharing your code.

Submitted by Tim on Tue, 09/21/2010 - 23:25

I updated an example, thanks for notices. As for passing a variable, this can be easily done, just a matter of taste.

Submitted by April on Sun, 09/19/2010 - 07:02

The error Rob mentioned comes up for me on nodes which have no taxonomies on them (ie I have one content type I need this for, and other content types I don't). It's because the foreach is being passed empty stuff. Can fix it with an if:

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;
}

Submitted by Tim on Tue, 09/21/2010 - 23:24

April, thanks for a suggestion, updated example.

Submitted by Matt on Thu, 09/23/2010 - 16:58

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

Submitted by Tim on Sat, 09/25/2010 - 12:26

U're welcome Matt! Glad that it is useful :)

Submitted by mike on Fri, 10/29/2010 - 04:18

Hi, great snippet thanks very much?

Any idea of how to modify it to display a comma separated list instead of a UL?

Submitted by Tim on Sat, 10/30/2010 - 17:13

Hey :)

Of course, just from a top of my head, quick and dirty approach (there maybe a cleaner variant):

replace this part of code

//theming terms out
foreach ($links AS $key => $vid) {
$terms[$key] = theme_links($vid);
}

with something like this:

foreach ($links AS $key => $vid) {
  foreach ($vid AS $link) {
       $terms[$key] .= l($link['title'], $link['href']).', ';
  } 
  $terms[$key] = substr($terms[$key], 0, -2);
}

This should work. Hope it helps :)

Submitted by Anonymous on Wed, 11/24/2010 - 11:56

Thanks, great snippet

Submitted by Simon on Thu, 02/17/2011 - 19:28

Any updates on this code for D7?

Submitted by veronika on Mon, 03/21/2011 - 19:23

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?

Submitted by Smtia on Tue, 04/26/2011 - 22:35

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.

Submitted by Carl on Thu, 06/02/2011 - 15:10

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:

http://api.drupal.org/api/drupal/themes--bartik--template.php/function/b...

Any chance you can help me?

Submitted by Tim on Mon, 06/20/2011 - 11:34

Submitted by Chris on Sat, 06/18/2011 - 09:28

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.

Submitted by Tim on Sat, 06/18/2011 - 12:46

On Monday I will post a tutorial detailing how to achieve this. Stay tuned :)

Submitted by ronnie on Tue, 07/05/2011 - 23:23

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

Submitted by John on Wed, 09/14/2011 - 00:22

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

Submitted by John on Wed, 09/14/2011 - 01:51

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

    Submitted by chinita7 on Thu, 11/24/2011 - 20:13

    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

    Submitted by Tejas P Mehta on Mon, 01/16/2012 - 07:47

    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.

    Submitted by JP on Fri, 04/12/2013 - 23:50

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

    Submitted by Tim on Sat, 04/13/2013 - 14:03

    hey, you're weclome, glad it helped :)

    Add new comment

    You are here