Written by gerard on Monday 19 April 2010
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.
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 nid, title FROM {node} WHERE nid < %d 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.
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'>« " . 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])))) . " »</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.
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);}?>
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;}
?>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!
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