3

I have an image field called field_body_image

I want to render it in my tpl.php file with a custom image style. I am using the following code (from this thread) to do this:

<?php $view = field_view_field('node', $node, 'field_body_image', array('settings' => array('image_style' => 'thumbnail')));?>
<?php print render($view); ?>

However, I want to display the URL of the image. How can I do that?

This thread has a solution, but it is for user photos and so you cannot specify a custom field.

Image Url Formatter is a module that can do this, but I would rather achieve it without an extra module.

This page has some advice, but apparently it is not best practice.

4

You can do this by getting the path to the file.

<?php $file = file_load($node->field_body_image['und'][0]['fid']); ?>

Then render the path:

<?php print file_create_url($file->uri); ?>

I tested this and it works. In my case I had an event image in my local dev site and it rendered:

http://events.dev/sites/default/files/event-images/imagefield_7tgPJ7_0.jpg

In your tags, you did not specify Drupal 6 or 7 but my code above would be for Drupal 7.

  • This gives me errors on the screen if the image is missing. – Evanss Apr 20 '15 at 11:23
  • @jdln - You may need to wrap an if statement around it in that case, i.e. it's an optional field for example. – Danny Englander Apr 20 '15 at 14:55
-1

first enable devel module, and add this to top of your tpl file:

<?php dpm($node); ?>

then clear your cache, and refresh your node. then you probably see an orange box at top of your page, click to collapse it, and start looking for your image cck field (ex. field_products_image), click on that to see if there is any more array beneath that or not, what you are looking for is a string named "filepath". now you have to render your image URL like this:

<?php print $node->field_products_image[0]['filepath'] ?>

let me know if it works or not.

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.