Previous/Next Node in Drupal 7

By Bill Seremetis, 14 January, 2012

Sometimes it is usefull ot provide previous/next links from the node you are reading. Customers love it, people love it and it is more compact than a "Latest News" list from views.

There is somecode laying around the net for doing this sort of thing but most of it is for Drupal 6 or isn't very well written.

I adapted some of the snippets I found on the net to create the following:

function THEME_prevnext($nid, $ntype) {
$prev = db_query("SELECT nid, title FROM {node} WHERE nid $nid, ':ntype' => $ntype));
$next = db_query("SELECT nid, title FROM {node} WHERE nid > :nid AND type = :ntype AND status = 1 ORDER BY nid ASC LIMIT 1", array(':nid' => $nid, ':ntype' => $ntype));

$prev_link = FALSE;
$next_link = FALSE;

foreach ($prev as $prev_node) {
$prev_alias = drupal_lookup_path('alias', 'node' . $prev_node->nid);
if($prev_alias) {
$prev_link = "" .$prev_node->title. "";
}
else {
$prev_link = "" .$prev_node->title. "";
}
}

foreach ($next as $next_node) {
$next_alias = drupal_lookup_path('alias', 'node/' . $next_node->nid);
if($next_alias) {
$next_link = '' .$next_node->title. '';
}
else {
$next_link = "" . $next_node->title . "";
}
}

$output = '

    ';
    if($prev_link) $output .= '
    ' . t('Previous article: ') . $prev_link .'

    ';
    if($next_link) $output .= '
    ' . t('Next article: ') . $next_link .'

    ';
    $output .= '

';

return $output;
}
function THEME_prevnext($nid, $ntype) {
$prev = db_query("SELECT nid, title FROM {node} WHERE nid $nid, ':ntype' => $ntype));
$next = db_query("SELECT nid, title FROM {node} WHERE nid > :nid AND type = :ntype AND status = 1 ORDER BY nid ASC LIMIT 1", array(':nid' => $nid, ':ntype' => $ntype));

$prev_link = FALSE;
$next_link = FALSE;

foreach ($prev as $prev_node) {
$prev_alias = drupal_lookup_path('alias', 'node' . $prev_node->nid);
if($prev_alias) {
$prev_link = "" .$prev_node->title. "";
}
else {
$prev_link = "" .$prev_node->title. "";
}
}

foreach ($next as $next_node) {
$next_alias = drupal_lookup_path('alias', 'node/' . $next_node->nid);
if($next_alias) {
$next_link = '' .$next_node->title. '';
}
else {
$next_link = "" . $next_node->title . "";
}
}

$output = '

    ';
    if($prev_link) $output .= '
  • ' . t('Previous article: ') . $prev_link .'
  • ';
    if($next_link) $output .= '

  • ' . t('Next article: ') . $next_link .'
  • ';
    $output .= '

';

return $output;
}

The above code is placed inside the template.php file and it does several checks in order to work on sites that have or don't have aliases for their nodes.
It also works correctly when the user is in the first or last node of the site, something that could happen easily especially in small blogs.

It accepts two arguments, the node-id and the node-type. So the links it generates are for the specific node-type the user is currently viewing. This of course can be easily adapted to anything you want.

I print the links by using this command in node.tpl.php:

echo(dialogoi_fusion_prevnext($node->nid, $node->type));
echo(dialogoi_fusion_prevnext($node->nid, $node->type));

 Print render also works, but I'm not really sure whether it is the correct way of doing this:

print render(dialogoi_fusion_prevnext($node->nid, $node->type));