in Drupal 7, how can I get a list of nodes based on a certain filter , in a page template? for example, page--popular.tpl.php
for example, getting the latest 4 nodes with content type 'article' and taxonomy name 'news' ?
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. 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:
/**
* 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.