Rendering fields in node.tpl.php template - Drupal 7

This is the correct way to render out fields using the power of the Field API. This adheres to Drupal 7 standards, and will avoid issues with things like localisation.

By creating a custom node.tpl.php template for your content type you can output your fields as you wish using PHP.

Printing a field from your node

     print render($content['field_name_here']);

Printing an element of a field

Say you want to render out just the image path (URI), rather than the full image syntax. If you use 'manage display' it will output the entire image with all of it's attributes (<img> tag along with src, width, height, alt...)

// field_my_image is the name of the image field
  // using field_get_items() you can get the field values (respecting multilingual setup)
  $field_my_image = field_get_items('node', $node, 'field_my_image');
  // after you have the values, you can get the image URL (you can use foreach here)
  $my_image_url = file_create_url($field_my_image[0]['uri']);
  print $my_image_url;

Justin Chevallier

Justin Chevallier

Avid Drupal site builder & user for +10 years.


Submitted by akhil on

I was trying hard to do the above thing for last 1 day.

Working fine for me.

Thank you.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.