RSS

Newsletter

The CodeKarate Newsletter is the best way for technical ninjas to keep their swords sharpened. Don't worry, we won't flood your inbox and your email address will always remain private.
Back to Top

Theme an image field in Drupal 7

If you have a node with an image field in Drupal 7 and need to manually print the image, here is a quick code snippet for theming the image:

$my_image = array(
  'style_name' => 'my_style', //the image style created
  'path' => $node->field_my_image['und'][0]['uri'],
);
 
print theme('image_style', $my_image);

As you will notice, the image field is called "my_image" and is accessible from $node->field_my_image['und'][0]

You will need to make sure you set up "my_style" inside the Image Styles section in the D7 Admin interface.

Discussions

1
Mohammed (not verified)

Hi,. Thanks for info but how

Hi,. Thanks for info but how can load all images in this field.. if we have like 5 images..
Thanks

1-1
shane

This might help

I am not sure exactly what you are trying to do, but if you just need to print multiple images from the same field you can probably use a foreach loop.

Something like this might work:

foreach ($node->field_my_image['und'] AS $image) {
  $my_image = array(
    'style_name' => 'my_style', //the image style created
    'path' => $image['uri'],
  );
 
  print theme('image_style', $my_image);
}

shane's picture

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h2> <h3> <blockquote> <img>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <php>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <mysql>, <python>, <ruby>. PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.