I have a template file for my blog page that renders a single image when the blog posts are listed. Originally, the field was for a single image, but it has been changed to accept more than one image and render only the first image. I would like to do within a template file instead of a view.
In that template file I used the following code, which doesn't work anymore since the image field has been changed.
<div class="image"><?php print render($content['field_blog_images']); ?></div>
Instead of that, I tried the following code, which doesn't work too.
<div class="image"><?php print render($content['field_blog_images'][0]); ?></div>
How should I render the first image of a field that accepts multiple images?
$images = field_get_items('node', $node, 'field_blog_images');
and then render the image:$blog_image = field_view_value('node', $node, 'field_blog_images', $images[0]); print render($blog_image);
Take a look at the example. Moreover, as @webkenny wrote in his answer, you should keep the logic away from the template files.$node = $variables['node'];
after that you'll be able to get the first image as I wrote in previous comment.