News + Insight feed2

A blog about people, ideas & creating the world we believe in

Multiple Vocabularies, Drupal Taxonomy Images & Contemplate

April 07, 2009

Multiple Vocabularies, Drupal Taxonomy Images & Contemplate

Every once and a while a design comes across my desk that looks easy but ends up being a challenge to code.

Right now I'm working on a Drupal project that has multiple Vocabularies. Users need to be able to select the terms that describe the product, and the corresponding term image needs to be displayed on the page.

Simple, right? That's what my designer thought.

Taking Control of Drupal Images

By default, Drupal's Taxonomy Image dumps all your images in one place, reguardless of how many Vocabularies you have. If you need more finite control, you have to theme your pages with the Content Templates (Contemplate) module.

Getting Specific with Vocabulary Images

So, you have multiple Vocabularies, and each of these Vocabularies has multiple Terms, and each Term has it's own Taxonomy Image. What's the best way to display just the Taxonomy Images for the terms selected within a specific Vocabulary using Contemplate?

Coding the Solution

Place this code in Contemplate body or teaser where you want the Taxonomy image to show up. Tested in Drupal 6.

if (count($taxonomy)):
// replace '5' with your vocabulary ID
$terms = taxonomy_node_get_terms_by_vocabulary($node, 5);
if ($terms) {
foreach ($terms as $key => $term) {
$items5[] = taxonomy_image_display($term->tid);
print implode($items5);
?> </code>

I hope this is helpful to you. Let me know in the comments below if this was something you were looking for, or if there are any ways you could improve upon it.

Read more Primal News