简体   繁体   English

以编程方式读取drupal7中的节点字段值

[英]Reading node field values in drupal7 programmatically

I am looking for the best way to get a field value from a node id. 我正在寻找从节点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? drupal函数中是否有任何构建可以解决这个问题?

Thanks @Berdir. 谢谢@Berdir。 I agree field_get_items is a better way. 我同意field_get_items是一种更好的方法。 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 . 不是全部,但你应该能够通过http://api.drupal.org/api/drupal/modules--field--field.field.module/function/field_get_items/7简化它。

You still need to check if $items[0] exists and get the 'value' of that. 您仍然需要检查$ items [0]是否存在并获得该值的“值”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM