Add a previous and a next link for your articles

Estimated reading time of this article: 3 minutes

Do you want link your article with the previous and the next article in Drupal 7?

Put the following code in your template.php of your theme:

<?php
/**
 * Gets the context nodes: start, prev, next and last.
 * @param $variables
 *  start, prev, next and last will be added. Passed by reference.
 * @param $texts
 *  array with start, prev, next and last
 *  having keys dominant_text, prepend_text and append_text
 * @return
 *  $variables with new values
 *
 * Reference: http://drupal.org/node/37767.
 */
function YOUR_THEME_context_nodes($node, &$variables, $texts = array()) {
 
$query = "(SELECT 'prev' as id, n.nid, n.title FROM {node} n WHERE "
   
. 'n.created < :created AND n.type = :type AND n.status = 1 '
   
. "AND language IN (:lang, 'und') "
   
. 'ORDER BY n.created DESC LIMIT 1) ';
 
$query .= ' UNION '
   
. "(SELECT 'next' as id, n.nid, n.title FROM {node} n WHERE "
   
. 'n.created > :created AND n.type = :type AND n.status = 1 '
   
. "AND language IN (:lang, 'und') "
   
. 'ORDER BY n.created ASC LIMIT 1)';
 
$query .= ' UNION '
   
. "(SELECT 'start' as id, n.nid, n.title FROM {node} n WHERE "
   
. 'n.type = :type AND n.status = 1 '
   
. "AND language IN (:lang, 'und') "
   
. 'ORDER BY n.created ASC LIMIT 1)';
 
$query .= ' UNION '
   
. "(SELECT 'last' as id, n.nid, n.title FROM {node} n WHERE "
   
. 'n.type = :type AND n.status = 1 '
   
. "AND language IN (:lang, 'und') "
   
. 'ORDER BY n.created DESC LIMIT 1)';

 
$rows = db_query($query, array(':created' => $node->created, ':type' => $node->type, ':lang' => $node->language));

  foreach (
$rows as $row) {
   
drupal_add_html_head_link(array(
     
'rel' => $row->id,
     
'type' => 'text/html',
     
'title' => $row->title,
     
'href' => url('node/' . $row->nid, array('absolute' => FALSE)),
    ));
    if (isset(
$texts[$row->id])) {
     
$text = !empty($texts[$row->id]['dominant_text']) ? t($texts[$row->id]['dominant_text']) : $row->title;
     
$variables[$row->id] = (!empty($texts[$row->id]['prepend_text']) ? t($texts[$row->id]['prepend_text']) : '')
        .
l($text, 'node/' . $row->nid, array('attributes' => array('rel' => array($row->id))))
        . (!empty(
$texts[$row->id]['append_text']) ? t($texts[$row->id]['append_text']) : '');
    }
  }
  return
$variables;
}
?>

Put the following code in your template.php of your theme:

<?php
function YOUR_THEME_preprocess_node(&$variables) {
   
YOUR_THEME_context_nodes($node, $variables, array(
     
'start' => array('dominant_text' => '« First article'),
     
'prev' => array('dominant_text' => '‹ Previous article'),
     
'next' => array('dominant_text' => 'Next article' '),
      '
last' => array('dominant_text' => 'Latest article' »'),
    ));
?>

Finally, you can output your links in your node.tpl.php of your theme:

    <?php if ($page && (isset($prev) || isset($next))): ?>
      <div id="prev-next" class="prev-next clearfix">
        <?php if (isset($prev)): ?>
          <div class="prev">
            <?php print $prev; ?>
          </div>
        <?php endif; ?>
        <?php if (isset($next)): ?>
          <div class="next">
            <?php print $next; ?>
          </div>
        <?php endif; ?>
      </div>
    <?php endif; ?>