News + Insight feed2

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

Multiple Vocabularies, Drupal Taxonomy Images & Contemplate

April 07, 2009 comments5 Comments

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.

<code>
<?php
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);
}
endif;
?> </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

Comments

Leave a comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This captcha is to verify you have a heart.
We screen every comment prior to publishing to verify it is on topic.

Thanks for this code, I was wondering how you would just display the term from the vocabulary without an image? Thanks

That was exactly what I was looking for Adam. Thanks for posting the code. ~ Bill

I don't think is a hard next step (I think you may have to use another taxonomy_image_get_object - http://drupal.org/node/220864) but I'm trying to also link these images. Any help?

Thanks for the code! It's just what I need.

Thank you for sharing this code snippet, I was little stuck without your solution. - Spletne strani