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:

/**
 * 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:

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:

    <div class="codeblock"><pre><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">if (</span><span style="color: #0000BB">$page </span><span style="color: #007700">&amp;&amp; (isset(</span><span style="color: #0000BB">$prev</span><span style="color: #007700">) || isset(</span><span style="color: #0000BB">$next</span><span style="color: #007700">))): </span><span style="color: #0000BB">?&gt;</span></code></pre></pre></div>
      <div id="prev-next" class="prev-next clearfix">
        <div class="codeblock"><pre><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">if (isset(</span><span style="color: #0000BB">$prev</span><span style="color: #007700">)): </span><span style="color: #0000BB">?&gt;</span></code></pre></pre></div>
          <div class="prev">
            <div class="codeblock"><pre><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">print </span><span style="color: #0000BB">$prev</span><span style="color: #007700">; </span><span style="color: #0000BB">?&gt;</span></code></pre></pre></div>
          </div>
        <div class="codeblock"><pre><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">endif; </span><span style="color: #0000BB">?&gt;</span></code></pre></pre></div>
        <div class="codeblock"><pre><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">if (isset(</span><span style="color: #0000BB">$next</span><span style="color: #007700">)): </span><span style="color: #0000BB">?&gt;</span></code></pre></pre></div>
          <div class="next">
            <div class="codeblock"><pre><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">print </span><span style="color: #0000BB">$next</span><span style="color: #007700">; </span><span style="color: #0000BB">?&gt;</span></code></pre></pre></div>
          </div>
        <div class="codeblock"><pre><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">endif; </span><span style="color: #0000BB">?&gt;</span></code></pre></pre></div>
      </div>
    <div class="codeblock"><pre><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">endif; </span><span style="color: #0000BB">?&gt;</span></code></pre></pre></div>