0

In a taxonomy I have some fields for the terms. To print out 1 of these fields in a node.tpl I found this code:

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

But this way I get only the first value - there are multiple terms selected.

How can I get all selected terms printed out?

1

0 in $node->field_hotel_tx_atmosphere['und'][0]['tid'] means that you are taking only first term so you should loop through array to get all terms.

I recommend entity_metadata_wrapper (available with Entity API module):

$wrapper = entity_metadata_wrapper('node', $node);
foreach ($wrapper->field_hotel_tx_atmosphere->getIterator() as $delta => $term_wrapper) {
  // prepare and render field value:
  $result = field_view_field('taxonomy_term', $term_wrapper->value(), 'field_tx_headline', 'text');
  print render($result);
}

or build a View to display selected data in the right way.

| improve this answer | |
  • Thank you, zaporylie, it works. But it renders all terms in 1 line. I'd like to have every single term in a div or span. – deelite Jan 27 '15 at 15:32
  • I got it: <?php $wrapper = entity_metadata_wrapper('node', $node); foreach ($wrapper->field_hotel_tx_accomondation->getIterator() as $delta => $term_wrapper) { $result = field_view_field('taxonomy_term', $term_wrapper->value(), 'field_tx_headline', 'text'); print '<li>' . render($result) . '</li>'; } ?> – deelite Jan 27 '15 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.