By Coupon Code Swap on
I have added some fields to a taxonomy vocabulary. I would like to print the associated fields for any terms that the node has been tagged with in the node.tpl template. How can I print term fields in the node.tpl template?
Comments
It would be very useful to
It would be very useful to print taxonomy fields from a term that has been chosen for a node. For example, you have added an image field to a vocabulary so that each term can be assigned an image. Now, you want the corresponding term image to be printed in the node every time a term is selected for the node.
There must be a way to do this. Any ideas?
Updated
Here is an updated snippet that is working and checks if the field has a value:
<?php $term=taxonomy_term_load($tid);$value=field_get_items('taxonomy_term',$term,'field_example');if($value){$img=field_view_value('taxonomy_term',$term,'field_example',$value[0],array('type'=>'image'));echo render($img);}?>Solved
<?php $term=taxonomy_term_load($node->field_example['und'][0]['tid']);$result=field_view_field('taxonomy_term',$term,'field_desired_field');echo render($result)?>Thanks!
Hi,
Works like a charm...
Just to make this code more clear:
I used it in node.tpl.php
my taxonomy field name (inside a node) was: field_tags
and my image field in the taxonomy vocabulary was: field_image,
so my final code was:
Perry.
Not working here.
Not working here.
working just fine here (d7).
working just fine here (d7). Many thanks !
worked for me un D7 as
worked for me un D7 as well.... thank you very mucho Perry for being so clear....
Implement hook_preprocess_node()
I added Term fields to node template by implementing hook_preprocess_node(). Additionally you can change the node--product.tpl.php but in my case it was not needed. There are two examples.
One small problem ...
@prabhatjn
I can second that, works like a charm!
One problem though, it just prints the image. How could it print the rendered image?
i.e. the code prints the Label and is not formatted by the Manage Display tab.
Thank you,
Same problem here: I need
Same problem here: I need print only field value not label.
---
Update:
I found solution. You need to add 4th parameter. After it prabhatjn's code will looks like this:
My was text:
Nice job community :D
If you want to utilize image styles
I wanted to use image styles in my templates, and after plenty of trial and error, I was able to get the following to work.
The goal was to print the "field_logo" from the taxonomy term "blog_series" associated with blog nodes, utilizing the "blog_series" image style.
For template.php:
Then for my node--blog.tpl.php, which first will check for the Blog Series logo, and if that's not there it'll look for the user's picture (from code that's not included above), and if that's not there it'll use a default logo for the site instead.
In my case
In my case
works. But I get only 1 term. I need all selected terms of a vocabulary. How can I get this?
Take note of making sure that
Take note of making sure that you have checks if the field exists. I had to look at my nodes for one field but because this field is not set in every node, I had to wrap if in an if else statement so no error messages would pop up: