node_load()
然后将字段作为属性访问是正确的方法,尽管我会稍有不同以避免对语言环境进行硬编码:
$lang = LANGUAGE_NONE;
$node = node_load($nid);
$url = $node->url[$lang][0]['value'];
你用来获取nid的方法是一种特别笨拙的方法。我将专注于重构并使用EntityFieldQuery
and entity_load()代替:
$query = new EntityFieldQuery;
$result = $query
->entityCondition('entity_type', 'node')
->propertyCondition('type', $node_type)
->propertyCondition('title', $title)
->execute();
// $result['node'] contains a list of nids where the title matches
if (!empty($result['node']) {
// You could use node_load_multiple() instead of entity_load() for nodes
$nodes = entity_load('node', $result['node']);
}
您尤其希望这样做,因为 title 不是唯一属性,并且如果该字段出现在节点以外的实体上。在这种情况下,您将删除entityCondition()
.