0

I created a custom content type with a field with multiple values which I want to render as follows in my node--custom_content.tpl.php;

<div class="CLASS" style="background: url('<?php print file_create_url($node->field_creation_images['und'][0]['uri']);; ?>')"></div>

However, this renders only the first value of the field. Is there a foreach solution to my problem?

1
  • 2
    Please look at the code yourself. Especially at this [0] part.
    – Mołot
    Commented Dec 6, 2014 at 1:01

1 Answer 1

1

For multivalue field it makes the field value array like :

// $node->field_creation_images['und'][0]['uri'];
// $node->field_creation_images['und'][1]['uri'];

So to print all values you need to use foreach :

<?php foreach ($node->field_creation_images['und'] as $uri) { ?>
  <div class="CLASS" style="background: url('<?php print file_create_url($uri['uri']); ?>')"></div> 
<?php } ?>
3
  • Works like a charm! Commented Dec 5, 2014 at 21:29
  • Don't use raw data; try using the Drupal functions instead, like in drupal.stackexchange.com/questions/15376/… Commented Jan 13, 2016 at 14:14
  • this is not the correct answer as there are many issues with this (it won't even work on multi-lang site). Google "correct way to render fields in drupal" and click the first link. Commented Aug 26, 2016 at 11:46

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