node_load显示不正确的数据

时间:2013-09-04 11:11:23

标签: drupal-7 hook nodes

我是Drupal 7的新手。

我正在尝试加载其类型和标题作为参数传递的特定节点的数据:

$param = array(
'type' => 'media',
'title' => 'Home Logo Bottom Image',
'status' => 1,
);

// Getting node details
$result = node_load($param);
echo "<pre>";
print_r($result);

stdClass Object
(
    [vid] => 1
    [uid] => 1
    [title] => Career Tip 1
    [log] => 
    [status] => 1
    [comment] => 2
    [promote] => 1
    [sticky] => 0
    [nid] => 1
    [type] => career_tips
    [language] => und
    [created] => 1377871907
    [changed] => 1377871907
    [tnid] => 0
    [translate] => 0
    [revision_timestamp] => 1377871907
    [revision_uid] => 1
    [body] => Array
        (
            [und] => Array
                (
                    [0] => Array
                        (
                            [value] => If you meet a woman doing a STEM job that sounds even remotely interesting to you, see if you can stop by her office for an “informational interview.” At the meeting, ask her every single question you have, even if they seem obvious or silly.
                            [summary] => 
                            [format] => full_html
                            [safe_value] => 
If you meet a woman doing a STEM job that sounds even remotely interesting to you, see if you can stop by her office for an “informational interview.” At the meeting, ask her every single question you have, even if they seem obvious or silly.


                            [safe_summary] => 
                        )

                )

        )

上面代码的输出不正确,因为它显示了一些其他标题。 我在这里缺少什么?

此外,我想获取同一节点的自定义字段值。那么有一个API可以返回整个数据吗?

2 个答案:

答案 0 :(得分:1)

drupal 7中node_load函数的行为发生了变化。传递给node_load的第一个参数必须是节点id。

使用EntityFieldQuery获取与查询匹配的所有节点ID,然后使用node_load($ nid)加载节点;

$query = new EntityFieldQuery();
$title = 'Enter the title of the node you want to search for here';
$query->entityCondition('entity_type', 'node')
  ->entityCondition('bundle', 'career_tips')
  ->propertyCondition('status', 1)
  ->propertyCondition('title', $title);

$result = $query->execute();

if (isset($result['node'])) {
  $node_nids = array_keys($result['node']);
  $items = entity_load('node', $node_nids);
}

// Now $items should contain the nodes.

此外,一旦有了节点对象,就可以使用EntityMetadataWrapper方便地提取值。

答案 1 :(得分:1)

您也可以使用node_load_multiple函数,但不推荐使用它。第二个参数是条件。

$node = current(node_load_multiple(array(), array(
    'title' => 'Home Logo Bottom Image',
)));