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

Coupon Code Swap’s picture

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?

Coupon Code Swap’s picture

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

Coupon Code Swap’s picture

<?php $term=taxonomy_term_load($node->field_example['und'][0]['tid']);$result=field_view_field('taxonomy_term',$term,'field_desired_field');echo render($result)?>

prabhatjn’s picture

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:

$term=taxonomy_term_load($node->field_tags['und'][0]['tid']);
$result=field_view_field('taxonomy_term',$term,'field_image');
echo render($result)

Perry.

TelFiRE’s picture

Not working here.

xaa’s picture

working just fine here (d7). Many thanks !

sincronista’s picture

worked for me un D7 as well.... thank you very mucho Perry for being so clear....

wzoom’s picture

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.

  1. First replaces node image field image styles and make first image use bigger image than the following images. You can use Shadowbox/Lightbox and it will work, because only styles were changed.
  2. Second example adds Term image field to the node template simply by changing markup of the term reference field.
function mytheme_preprocess_node(&$variables) {

	if ($variables['node']->type != 'product') return;
	
	// Replace 2nd, 3rd,...N-th images with smaller ones
	if (!empty($variables['content']['field_product_image'])) {
	   foreach(element_children($variables['content']['field_product_image']) as $key) {
		 	if ($key == 0) continue; // Skip first image - keep it big

		 	$variables['content']['field_product_image'][$key]['#image_style'] = 'small_square';
	 	}
	}

	//dsm($variables, 'vars');
	
	// Add image to Term Title
	if (!empty($variables['content']['field_product_my_term_ref'])) {
	   foreach(element_children($variables['content']['field_product_my_term_ref']) as $key) {
		 	$term = $variables['content']['field_product_my_term_ref']['#items'][$key]['taxonomy_term'];
		 	if (isset($term->field_term_image[LANGUAGE_NONE][0])) {
			 	$item = &$variables['content']['field_product_my_term_ref'][$key];
		 		$img = theme('image_style', array(
		 			'style_name' => 'small_square', 
		 			'path' => $term->field_term_image[LANGUAGE_NONE][0]['uri'],
		 		));
		 		
		 		$item['#prefix'] = $img.'<div class="title">';
		 		$item['#suffix'] = '</div>';
  			}
	 	}
	}
}

IamOnStage’s picture

@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,

jasom’s picture

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:

$term=taxonomy_term_load($node->field_tags['und'][0]['tid']);
$result=field_view_field('taxonomy_term',$term,'field_image','image');
print render($result);

My was text:

$term=taxonomy_term_load($node->field_country['und'][0]['tid']);
$result=field_view_field('taxonomy_term',$term,'field_iso2_country','text');
print render($result);

Nice job community :D

nwehner’s picture

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:

<?php 
  if (($node->type) == 'blog') {
    $blog_series_node = field_get_items('node', $node, 'field_blog_series'); // find the blog series term from the node
    $blog_series_tid = $blog_series_node[0]['tid']; // get the term id
      if (isset($blog_series_tid)) { // do isset checks to make sure you don't get any exemptions
        $blog_series_term = taxonomy_term_load($blog_series_tid); //so there are no exemptions with the next line
        $blog_series_items = field_get_items('taxonomy_term', $blog_series_term, 'field_logo');
        $blog_series_logo_items = $blog_series_items[0]['uri'];
          if (isset($blog_series_logo_items)) {
            $vars['blog_series_logo'] = image_style_url('blog_series', $blog_series_logo_items);
      }
    }
  }
?>

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.

  <?php if (isset($blog_series_logo)): ?>
    <?php print "<img src='$blog_series_logo' alt='Blog series logo' height='180' width='180'/>"; ?>
  <?php elseif (isset($blogger_photo)): ?>
    <?php print "<img src='$blogger_photo' alt='Blogger picture' height='180' width='180'/>"; ?>    
  <?php else: ?>
    <?php print "<img src='/sites/default/files/logo/OC-Blog.png' alt='Blogs' height='180' width='180'/>"; ?>
  <?php endif; ?>
deelite’s picture

In my case

<?php $term=taxonomy_term_load($node->field_hotel_tx_atmosphere['und'][0]['tid']);
$result=field_view_field('taxonomy_term',$term,'field_tx_headline','text');
print render($result); ?>

works. But I get only 1 term. I need all selected terms of a vocabulary. How can I get this?

rcls’s picture

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:

<?php if($node->field_category): ?>
  <?php $term = taxonomy_term_load($node->field_category['und'][0]['tid']); ?>
  <?php if($term): ?>
    <?=$term->description?>
  <?php endif; ?>
  <?php else: ?>
  No category
<?php endif; ?>