123*_*123 2 php drupal drupal-7
现在,如果节点类型是文章.在术语页面左侧,我想显示最新的10篇文章的标题,哪个节点类型是文章.我不想使用意见,我该怎么办?谢谢.
如果我想在节点页面的左边显示最新的10篇文章的标题,哪个节点类型是文章.如何编写查询.非常感谢.
ps:我发现EntityFieldQuery也许可以做到这一点,但我现在不知道如何做到这一点.
我的代码:
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1)
->propertyOrderBy('created', 'DESC')
->range(0, 10);
$result = $query->execute();
Run Code Online (Sandbox Code Playgroud)
代码可以是这样的(使用db_select())
$query = db_select("node", "n") // select from the node table
->fields("n", array("nid", "title")) // fields nid, title
->condition("type", "page", "=") // where the node type = page
->orderBy("created", "DESC") // order by the newest
->range(0, 10) // select only 10 records
->execute(); // execute the query
while($record = $query->fetchAssoc()) {
print(l($record['title'], "node/" . $record['nid'])); // print the node title linked to node.
}
Run Code Online (Sandbox Code Playgroud)
使用EntityFieldQuery()的另一个例子:
$query = new EntityFieldQuery();
$entities = $query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'club')
->propertyOrderBy("created", "DESC")
->range(0, 10)
->execute();
foreach($entities['node'] as $obj)
{
$node = node_load($obj->nid);
print(l($node->title, "node/" . $node->nid));
}
Run Code Online (Sandbox Code Playgroud)
性能明智:使用第一种方法.
希望这有助于......穆罕默德.