Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drupal 7: Access custom node field in page.tpl.php

I added a new field "mood" (image) to the page content type. Is there any way to access the image stored in this field in the page.tpl.php?

like image 510
dantz Avatar asked Jan 13 '11 15:01

dantz


2 Answers

Should be

$node = node_load($nid);
$node->field_mood[$node->language][0]['value'];
like image 150
JochenJung Avatar answered Oct 15 '22 02:10

JochenJung


There is a new "field_get_items()" function in drupal 7. The $node variable should already be defined in page.tpl so the first line may not be required.

This will get the field in the appropriate language. There is also an optional parameter to specify the desired language if needed.

$node = node_load($nid);
$values = field_get_items('node', $node, 'mood');
if ($values != FALSE) {
  $val = $values[0]['value'];
}
else {
  // no result
}

reference: http://api.drupal.org/api/drupal/modules--field--field.module/function/field_get_items/7

like image 45
idFlood Avatar answered Oct 15 '22 01:10

idFlood