简体   繁体   English

Drupal。 获取模板中的过滤节点

[英]Drupal. Get filtered nodes in template

in Drupal 7, how can I get a list of nodes based on a certain filter , in a page template? 在Drupal 7中,如何在页面模板中基于某个过滤器获取节点列表? for example, page--popular.tpl.php 例如,page--popular.tpl.php

for example, getting the latest 4 nodes with content type 'article' and taxonomy name 'news' ? 例如,获取内容类型为“ article”和分类名称为“ news”的最新4个节点?

I know most people do this in 'views' but there are reasons that I can't do this. 我知道大多数人都在“视图”中执行此操作,但是有一些原因导致我无法执行此操作。

Appreciate if anyone can help! 感谢任何人都可以提供帮助!

Page templates contain regions, in particular, already rendered content region. 页面模板包含区域,尤其是已经渲染的content区域。 So, I suppose, that your question must be correctly formulated as follows: "How can I make a custom page containing list of nodes, without using Views". 因此,我想,您的问题必须正确地表述为:“如何在不使用视图的情况下创建包含节点列表的自定义页面”。 To do so, you need to implement hook_menu in your module: 为此,您需要在模块中实现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().
  }
}

Take a look at hook_menu and How to use EntityFieldQuery . 看一下hook_menu如何使用EntityFieldQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM