Printing or Rendering a Node Field or Profile2 Field in Drupal 7

When overriding a node template or user profile, you should not be drilling down the field array and printing the output unless you have a very good reason to do so. i.e. (do not do this) print $node->field_zipcode['und']['0']['value'];

The Drupal 7 field api offers us the field_view_field() function to get a renderable array of the output for a field entity.

Using this method also give you access to whatever settings the field has setup in the "Manage Display" tab of the entity.

This is an example of how to print a node field named "field_zipcode".

<?phpprint drupal_render(field_view_field('node', $node, 'field_zipcode'));?>

In that example, 'node' is the entity type. field_view_field() works with other entity types as well, such as profile2.

Here is an example of how to print a profile2 field named 'field_zipcode' that exists in the 'main' profile2 type.

<?php$profile = profile2_load_by_user($account);print drupal_render(field_view_field('profile2', $profile['main'], 'field_zipcode'));?>