Displaying a previous - next post link in your Drupal theme

If you're a convert to Drupal from a blogging platform like WordPress, you might miss the Previous/Next post links that allow visitors to read through the posts on your site in a chronological manner.

I've been searching for a solution to this for a while, but nothing I've found on Drupal.org has offered an acceptable solution for me. What I'm going to show you here is some code I've put together that retrieves the next and previous posts from the database and displays them for the user. If you are a Drupal guru and have suggestions for how to refine this, please share your knowledge in the comments.

Step 1: Mining The Database!

Before we get to the presentational aspects of the code, we need to find out what "nodes" come immediately before and after our post. Here's the line of SQL code I'm using to extract the information:

<?phpSELECT nidtitle FROM {nodeWHERE nid &lt; %AND type '%s' AND status 1 ORDER BY nid DESC LIMIT 1?>

Explanation: This line retrieves the node before the current one. The "type = '%s'" allows this to only select from the same node type. This is useful if you have several different content types on your site, and you only want the script to select from the same type of content.

The "status" portion ensures only published nodes are shown, and the "limit" ensures that only one record is selected.

Step 2: Editing template.php

Jumping swiftly along, here's the code I added to my template.php file.

<?phpfunction yourtheme_prevnext($nid,$ntype) {
    
//This selects the previous record and builds it into an HTML string using the Drupal "l" function to build the link
    
$result db_query("SELECT nid, title FROM {node} WHERE nid < %d AND type = '%s' AND status = 1 ORDER BY nid DESC LIMIT 1",$nid,$ntype);
    while (
$row db_fetch_array($result)) {
      
$outputstring .= "<div class='prevpost'>&laquo; " l(t($row[title]),"node/".$row[nid], array(attributes => array('title'=> t($row[title])))) . "</div>";
    }
    
//This selects the next record and builds it into an HTML string as in the previous example
    
$result db_query("SELECT nid, title FROM {node} WHERE nid > %d AND type = '%s' AND status = 1 ORDER BY nid ASC LIMIT 1",$nid,$ntype);
    while (
$row db_fetch_array($result)) {
    
//Finally, we wrap the output in a containing DIV element for themeing purposes
    
$outputstring .= "<div class='nextpost'>" l(t($row[title]),"node/".$row[nid], array(attributes => array('title'=> t($row[title])))) . " &raquo;</div>";
    }
    
$outputstring "<div class='post-navigation clearfix'>" $outputstring "</div>";
    print 
$outputstring;
}
?>

This selects the previous and next records from the database and wraps them up in HTML. We use the Drupal <?phpl()?> function to create the links in the correct way.

Step 3: Call the function from node.tpl.php

OK, in order to get the HTML to display, you have to edit your <?phpnode.tpl.php?> file. In the code snippet below, I'm going to call the code directly, but in reality, I only display this code when you're looking at the full node, not the teaser view you might see in a category list.

<?php yourtheme_prevnext($node->nid,$node->type);?>

The code to only display prevnext on full nodes is below

<?php if (!$page == 0) {yourtheme_prevnext($node->nid,$node->type);}?>

Step 4: Add some styling to your CSS file

Now, in order to make it look good, add some code to your stylesheet. I'll give you an example of what I used in my sandbox, but you may need to tweak it for your theme.

<?php/* Post Navigation */
.post-navigation {font-size:12.5px;}
.
prevpost {float:left;width:48%;text-align:left;}
.
nextpost {float:right;width:48%; text-align:right;}
?>

Your feedback

OK, I've shown you my code. I don't know if anybody else has attempted this, but I'm happy to see other solutions or suggestions for how the code may be improved for this theme.

Please share!

Subscribe with Add to Google or Subscribe to netvibes

Comments

You could also use if

You could also use if Previous/Next API if you don't want to make your hands dirty with programming. This module provides next/previous blocks. You can see it in action on my Drupal blog.

That's a gorgeous site, Davy

That's a gorgeous site, Davy - I love the design. And I actually came across your TOC post earlier today. I may have a use for that module in one of my own projects. Thanks for dropping by!

What about Custom pagers ?

What about Custom pagers ? It's a very flexible solution for the previous/next problem

Works great! There it is

Works great!

There it is working

http://www.lobbi.es/persona/casa-real/rey-don-juan-carlos

Thanks!!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <table> <thead> <tbody> <tr> <td> <h2> <h1> <h3> <h4> <h5> <div> <object> <embed> <param> <br>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <drupal5>, <drupal6>, <javascript>, <mysql>, <php>. The supported tag styles are: <foo>, [foo].
  • Links to specified hosts will have a rel="nofollow" added to them.

More information about formatting options

To prevent automated spam submissions leave this field empty.
Mollom CAPTCHA (play audio CAPTCHA)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.