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

狂风中的少年 提交于 2019-12-03 05:21:34

问题


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?


回答1:


Should be

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



回答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



来源:https://stackoverflow.com/questions/4681780/drupal-7-access-custom-node-field-in-page-tpl-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!