2016-05-03 6 views
0

Drupal 7で、ページテンプレート内の特定のフィルタに基づいてノードのリストを取得するにはどうすればよいですか?たとえば、page - popular.tpl.phpDrupal。テンプレートでフィルタリングされたノードを取得する

たとえば、コンテンツタイプ 'article'と分類名 'news'の最新の4ノードを取得しますか?

私はほとんどの人が「ビュー」でこれを行うことがわかっていますが、私はこれを行うことができない理由があります。

誰でも手伝ってもらえますか?

+0

「理由」を説明してください。 –

答えて

2

ページテンプレートには、特に領域が含まれており、すでにレンダリングされた領域は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を見てみましょう。

関連する問題