I am looking for the best way to get a field value from a node id.
My actually code works however I guess there is an easier way.
$node = node_load( 1 );
$lang = $node->language;
$field = 'body';
$value = '';
if ( isset($node->{$field}[$lang]) && isset($node->{$field}[$lang][0]) )
{
$value = $node->{$field}[$lang][0]['value'];
}
echo $value;
Is there any build in drupal function that takes care of this?
Thanks @Berdir. I agree field_get_items is a better way. Here is a code example:
<?php
$body = field_get_items('node',$node, 'body');
print $body[0]['value'];
?>
Not all of it, but you should be able to simplify it a bit with http://api.drupal.org/api/drupal/modules--field--field.module/function/field_get_items/7 .
You still need to check if $items[0] exists and get the 'value' of that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.