Correctly get fields
Use the field_get_items() function to get a node’s fields, as this respects the current language. Certainly don’t index directly into the array.
The following example gets the first item of an image field (with machine name ‘field_image’):
$image_fields = field_get_items('node', $node, 'field_image');
if ($image_fields) {
$first_image_field = $image_fields[0]; // First image
// do stuff
}
See field_get_items
References
Get a taxonomy term
Use taxonomy_term_load() to load a taxonomy term from a field.
For example, lets say we have a taxonomy that we’ve added as a term reference field to a content type. Assuming that the field’s machine name is ‘field_my_taxonomy’, we can get the name of the first item as follows:
function my_theme_preprocess_node(&$variables) {
$node = variables['node'];
$my_taxonomy = '';
$my_taxonomy_fields = field_get_items('node', $node, 'field_my_taxonomy');
if ($my_taxonomy_fields) {
$first_tid = $my_taxonomy_fields[0]['tid']; // tid of first term
$first_term = taxonomy_term_load($first_tid);
$first_term_name = $first_term->name;
}
}
See function taxonomy_term_load
References
Change image style of an image field
If you have an image field set on a node (with machine name ‘field_image’) and you’d like to change its image style, get the field and theme it using the uri of the image and the new style name (called ‘new_style’ in this example), as follows:
$image_fields = field_get_items('node', $node, 'field_image');
if ($image_fields) {
$image_field = $image_fields[0]; // First image
// Print the image in its new style
print theme('image_style', array('path' => $image_field['uri'], 'style_name' => 'new_style'));
}