【问题标题】:Drupal. Get filtered nodes in template德鲁巴。获取模板中过滤的节点
【发布时间】:2016-05-03 08:28:14
【问题描述】:

在 Drupal 7 中,如何在页面模板中获取基于某个过滤器的节点列表?比如page--popular.tpl.php

例如,获取内容类型为“文章”且分类名称为“新闻”的最新 4 个节点?

我知道大多数人在“视图”中这样做,但我不能这样做是有原因的。

如果有人可以提供帮助,不胜感激!

【问题讨论】:

  • 解释你的“原因”。

标签: php drupal drupal-7 drupal-theming


【解决方案1】:

页面模板包含区域,特别是已经呈现的content 区域。因此,我想,您的问题必须正确表述如下:“如何在不使用视图的情况下制作包含节点列表的自定义页面”。为此,您需要在模块中实现hook_menu

/**
 * Implements hook_menu().
 */
function mymodule_menu() {
  $items = array();

  $items['popular'] = array(
    'title' => 'Popular articles',
    'page callback' => '_mymodule_page_callback_popular',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Page callback for /popular page.
 */
function _mymodule_page_callback_popular() {

  $news_tid = 1; // This may be also taken from the URL or page arguments.

  $query = new EntityFieldQuery();

  $query->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'article')
    ->propertyCondition('status', NODE_PUBLISHED)
    ->fieldCondition('field_taxonomy', 'tid', $news_tid) // Use your field name
    ->propertyOrderBy('created', 'DESC')
    ->range(0, 4);

  $result = $query->execute();

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

    // Now do what you want with loaded nodes. For example, show list of nodes
    // using node_view_multiple().
  }
}

看看hook_menuHow to use EntityFieldQuery

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多