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

22,430

Solution 1

Should be

$node = node_load($nid);
$node->field_mood[$node->language][0]['value'];

Solution 2

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

Share:
22,430

Related videos on Youtube

dantz
Author by

dantz

Updated on July 09, 2022

Comments

  • dantz
    dantz almost 2 years

    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?

  • dantz
    dantz over 13 years
    Thanks. The $node->language was the missing piece in my puzzle.
  • Andrew Sledge
    Andrew Sledge over 13 years
    Remember that CCK has been moved into the core. It's no longer $node->field_language[0]['value']. The field_ prefix is no longer necessary.
  • joar
    joar almost 13 years
    How would I go about to render a field, just as it is possible in node.tpl.php? render($content['FIELD']) but instead somthing along the ways of render($node->FIELD)?
  • joar
    joar almost 13 years
    I found the solution to my comment, I had to use $view = node_view($node); render($view). This was a while ago, more information about node_view is out there.
  • Disco
    Disco over 12 years
    I have a 'Notice: Undefined variable: node' in page.tpl.php; D7.
  • Disco
    Disco over 12 years
    Ok seems that '$node' is not available in "front" page.
  • Capi Etheriel
    Capi Etheriel over 12 years
    if you're in the node page, please use <code>$node = menu_get_object();</code> since it gets the node from the page cache.

Related