How To Separate Taxonomy Terms Output By Vocabulary in Drupal 7

Jun 20, 2011 · by Tim Kamanin

Almost a year ago I wrote a tutorial about How To Separate Drupal Taxonomy Terms Output By Vocabulary (which is the third most popular post on this blog to date!), the technique desribed in the tutorial was useful for Drupal 6, but not for Drupal 7. I got some requests to explain How To Separate Taxonomy Terms Output By Vocabulary in Drupal 7 and here it is. The explanation. In Drupal 7 a lot of things changed including the way how we attach taxonomies to nodes and display node 's contents. Taxonomies are now attached as fields , so after creating a Vocabulary you:

  • a) Go straight to Content Types - > _Content Type Name_ - > Manage Field and add a field of Term reference type there.
  • b) Name it as you wish and remember its name, in my case I called the field _' field_category'_ (clever, huh?)
  • c) Next you choose the vocabulary which terms are going to be referenced via this field.

Note the difference between D6 and D7? Now you can add as much 'term reference' fields as you want and each Term reference field means a separate Vocabulary.

  • d) So add a second Vocalbulary (for example, for tags) and create a second Term reference field for your content type, call it _' fields_tags'_ (for example). Choose a second vocabulary as a source for terms.

Ok, now if you create a node and assign terms from both vocabularies, you will note that terms aren't separated. How do we separate them and output in different parts of the theme?

  • e) Now the simplest part is left. Go to node.tpl.php and edit it. Put
print render($content['field_category']);

to any part of the template to output our first vocabulary and put

print render($content['field_tags']);

to output our second vocabulary. _One important thing to note: if you 're going to output one of the vocabularies after the main content render happens ( render($content); ), then you need to put this string before the main content output call: _

_hide($content[ 'field_tags']);

_(as an example). By doing this, you 're hiding a vocabulary from the main content output preventing it from unwanted rendering._

Well that's all, compared to Drupal 6, the approach in Drupal 7 became more flexible, intuitive and requires less code! Hooray to Drupal 7 :)

Want to get more 🔥 tips like this one?

Subscribe to get notified about new dev tutorials