现在,如果节点类型是文章。在term页面的左侧,我希望显示节点类型为文章的最新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();发布于 2012-11-25 16:31:54
代码可以是这样的(使用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.
}使用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));
}性能方面:使用第一种方法。
发布于 2012-11-25 18:55:03
另一个解决方案,我将提及,因为如果它是良好的Drupal知识。视图模块只需很少的工作就可以创建这样的块。这是一个有点棘手的学习,但它在很大程度上是为了让这些类型的清单。
发布于 2016-09-12 18:03:58
就D7性能而言,您最好使用旧的db_query命令:
$result = db_query("SELECT nid FROM {node} WHERE type = :type AND status = 1 ORDER BY created DESC LIMIT 10", array(':type' => $type));
foreach ($result as $record) {
// Do something with each $record
$node = node_load($record->nid);
}这是因为db_select和EntityFieldQuery()允许模块挂钩并修改查询。这对你来说可能是件好事!
我只是提供了一些选择。
https://stackoverflow.com/questions/13549448
复制相似问题