Adding next & previous links for same node type, translation-aware

Last modified: August 23, 2009 - 21:16

description

This snippet adds a custom next_prev_same_type function to your theme template.php.
This allows you to insert Next and Previous links in your *.tpl.php file.

This function selects previous/next items depending on the time the node was created with a single query.
Only published nodes of the same node type like the current node are selected.

If the next/previous node id retrieved from the database is one of the current nodes' translations,
next_prev_same_type runs again to skip that node. In this case, there will be a new sql query.

Step 1 of 2

Insert this code into your template.php

<?php
function next_prev_same_type($nid=null, $path=null, $direction=null)
{
  if(
$nid){
   
// gather some node information
   
$node = node_load($nid);
   
$type = $node->type;
   
$created = $node->created;
   
   
// retrieve all node ids of this nodes' translations
   
$translation_ids = array();
    foreach(
$node->translation as $translation) {
      if(
$translation->status == 1){
       
$translation_ids[$key] = $translation->nid;
      }
    }
   
   
// switch some vars depending on direction
   
switch ($direction){
      case
'next':
       
$modifier = ">";
       
$order = "ASC";
       
$link_str = "Next project ›";
        break;
      case
'prev':
      default:
       
$modifier = "<";
       
$order = "DESC";
       
$link_str = "‹ Previous project";
    }
   
   
// get next / prev element from database
   
$query = db_query('select nid from {node} where type=\'%s\' AND status=1 AND nid!=%d AND created'.$modifier.'%d ORDER BY created %s, nid %s Limit 1',$type,$nid,$created,$order,$order);
    while (
$item = db_fetch_object($query)){
      if(
$item->nid!=$nid && !in_array($item->nid,$translation_ids)){
       
$nav_nid = $item->nid;
      }else{
       
// skip element, if it is a translation of the current node
       
return next_prev_same_type($item->nid,$path,$direction);
      }
    }
    if(
$nav_nid){
      return
l(t($link_str),$path."/".$nav_nid);;
    }
  }
}
?>

Step 2 of 2

Insert the code below somewhere in your *.tpl.php file (node.tpl.php, view_xyz.tpl.php, ...)

<?php
  $next
= next_prev_same_type($node->nid, "your/path/here", "next");
 
$prev = next_prev_same_type($node->nid, "yout/path/here", "prev");
  if(
$prev!= FALSE){
   
$links[] = $prev;
  }
  if(
$next!= FALSE){
   
$links[] = $next;
  }
  print
theme_links($links, $delimiter = ' ')
?>

OR

<?php
 
print next_prev_same_type($node->nid, "your/path/here", "next");
  print
next_prev_same_type($node->nid, "yout/path/here", "prev");
?>

Format as desired.

function variables

next_prev_same_type($nid=null, $path=null, $direction=null)
$nid is the current node
$path is the path to be used in the link
$direction is either 'next' or 'prev'

Notes

  • Once the function is placed in your template.php, it can be run on any page, template, block, ...
  • Add your comments below. Any ideas or modifications are welcome.
 
 

Drupal is a registered trademark of Dries Buytaert.