3

I have a template file for my blog page that renders a single image when the blog posts are listed.

My problem is that originally this was for a single image field, but now I want to change it to multiple and render only the first image in the line. I would like to do within the template file and not in views.

<div class="image"><?php print render($content['field_blog_images']); ?></div>

Since I changed it to a multiple image field it doesnt work at all, so I tried like

<div class="image"><?php print render($content['field_blog_images'][0]); ?></div>

no avail.

Any clue appriciated.

B

  • If it's the multiple image field, first you need to get the array with the 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. – Jack-PL Jul 28 '13 at 21:29
  • I tried to do it as @user18400 suggested but it throws an error: Notice: Undefined variable: node in designmdd_preprocess_node() (line 68 of /home/heft57/public_html/sites/all/themes/designmdd/template.php). EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7663 of /home/heft57/public_html/includes/common.inc). – user18400 Jul 30 '13 at 11:55
  • This is a drupal 7 blog page modified with a template by the way; so my goal is to show the first value of the blog entry's 'field_blog_images' in the blog entry list (/blog) page as well. – user18400 Jul 30 '13 at 12:02
  • define the node object in your preprocess function $node = $variables['node']; after that you'll be able to get the first image as I wrote in previous comment. – Jack-PL Jul 30 '13 at 19:23
  • You are right sorry I missed that. But it still doesnt work. – user18400 Jul 31 '13 at 16:32
2

It's all about the structure of your imagefield. Notice you're automatically going to the 0 index but I bet if you were to do a dump of that variable, you'd find you were at the wrong position in your array. You'll need to loop through it or target the very specific element you're looking to render.

Check out this documentation comment for details and modify for your example. :)

https://api.drupal.org/api/drupal/modules!field!field.module/function/field_view_value/7#comment-20069

For that reason it could be worth taking this up to template.php in a preprocess_node hook so you're not doing all kinds of crazy logic in a template file.

Something like this:

function mytheme_preprocess_node(&$variables) {
  if($variables['node']->type == 'blog' {
    // My crazy logic to assign my image variable.
    $variables['blog_image'] = render($somevalue);
  }
}

Then in your tpl.php it's just:

print $blog_image;
| improve this answer | |
1

I got the answer to this question for Drupal 7 via https://stackoverflow.com/questions/3463147/getting-the-field-image-path-in-drupal-7

<img src="<?php echo render(file_create_url($node->field_image['und'][0]['uri'])); ?>" />
| improve this answer | |
0

If you insist on not to use Views for theming your blog teasers, you can use template preprocess_node function to create the hook suggestion and variables for the template file which you're going to use.

Example:

/**
 * Override or insert variables into the node template.
 */
function YOURTHEME_preprocess_node(&$variables) {

  $node = $variables['node'];

  // it allows to use templates for the type and view_mode e.g: node--blog--teaser.tpl.php
  $variables['theme_hook_suggestions'][] = 'node__' . $node->type . '__' . $variables['view_mode'];

  // blog teaser
  if ($node->type == 'blog' && $variables['view_mode'] == 'teaser') {
    $images = field_get_items('node', $node, 'field_blog_images');
    if ($images) {
      $blog_image = field_view_value('node', $node, 'field_blog_images', $images[0], 
        array(
          'type' => 'image',
          'settings' => array(
          'image_style' => 'thumbnail',
          'image_link' => 'content',
        ),
      ));
      $variables['blog_image'] = $blog_image; 
    }
  }
}

Now you can use node--blog--teaser.tpl.php file for theming your blog teasers and print the image:

<div class="image"><?php print render($blog_image); ?></div>
| improve this answer | |

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.