Saturday, October 8, 2011

List Recent Posts on Drupal 7 Using PHP

There's already a version 8 of Drupal but still, it's not easy to look for documentations of Drupal 7. They have a lot of methods from version 6 that I have to translate from my old code lately.

It almost frustrated me until I came up with the following code that works:

<?php
$query = db_select('node', 'n');

$nids = $query
    ->fields('n', array('nid'))
    ->orderBy('created', 'DESC')
    ->range(0, 7)
    ->execute()
    ->fetchCol('title');

$nodes = node_load_multiple($nids);
echo '<ul class="menu">';

foreach ($nodes as $recentNode) {
echo '<li class="first"><a href="?q='.drupal_lookup_path('alias',"node/".$recentNode->nid).'">'.$recentNode->title.'</a></li>';
}

echo '</ul>';

?>

No comments:

Post a Comment