Skip to content

Previous, Next links

I’ve written another plugin for WordPress 1.2, this time to generate the “Next Page” and “Previous Page” links at the top of this page. Actually, you can put them on the top, or on the bottom, or even in the side bar! It works to give you multiple pages of post archiving not just on the front page, but on each of your category archives, as well as on each of your date-based archives (assuming you have enough posts). I’m sure that a lot of WordPress users are wondering why I needed a plugin for that, since there is supposedly an option for it …

Well, the plain fact is, you have to edit your index.php page to get the links to show up, and when I tried the suggestions which were given on the support forums I got lots of database errors. I eventually did get the links to show up, but still with an error message. But even worse for me, was the fact that the links use query style strings instead of friendly URL’s.

I can understand using the queries in this case, since clearly, we don’t really care if the extra pages are visible to search engines, but it really didn’t fit in with the rest of my links. So I fixed it by writing this plugin


To use the plugin, you have to have “posts paged” chosen on your Front Page settings, and you’ll need to add some rules to your .htaccess if you want the pretty links (it could still work with query-style strings: ?paged=3 and so on).

So, you need to add the following rules (or ones like them) to the top of your .htaccess rewrite rules. Note that there are only 4 rules, one of them is line wrapped, but should be on one line in your files (each line starts with “RewriteRule”).

RewriteRule ^category/?(.*)/?page/?([0-9]{1,})?/? /index.php?category_name=&paged= [QSA,L]
RewriteRule ^author/?([^/]*)/?page/?([0-9]{1,})/? /index.php?author_name=&paged= [QSA,L]
RewriteRule ^([0-9]{4})/?([0-9]{1,2})?/?([0-9]{1,2})?/?page/?([0-9]{1,2})?/?$ /index.php?year=&monthnum=&day=&paged= [QSA,L]
RewriteRule ^page/?([0-9]{1,})/?$ /index.php?paged= [QSA,L]

If you don’t want to do that, or your server doesn’t support mod_rewrite, you’ll want to set the variable

$page_default = $page_querystring
in the plugin before installing it.

Then of course, you have to add code to your index.php to get the links:

<?php previous_index_page_link(); next_index_page_link(); ?>

They have classes applied: previousposts and nextposts, so that you can style them however you like, and you can optionally pass each of those functions the text that you want displayed in the link. For an example, here’s my styles:

div.indexpagenavlinks {
     height: 1em;
     display: block;
 }
div.indexpagenavlinks .previousposts {
     text-align: left;
     float: left;
 }
div.indexpagenavlinks .nextposts {
     text-align: right;
     float: right;
 }

If you’re still interested in trying it out [;-)] you can download it for yourself

8 Comments

  1. ian wrote:

    Neat !

    I have two comments to make: the first is that I had to change the first RewriteRule statement syntax to cope with sub-categories (basically it needs to be able to pass a / in $1), so it now looks like this:

    RewriteRule ^category/(.*)?page/?([0-9]{1,})?/? /index.php?category_name=$1&paged=$2 [QSA,L]

    and the second is that with the system as it stands, searching isn’t so hot. On a search for the letter ‘a’ (I wanted to make sure the paged limit was hit !) I end up with the following URL for the second page:

    http://my.demo.blog/index.php?s=a&paged&2

    which isn’t so great. I’m not sure yet why the = has become a 2, but so far that’s the only real show stopper from taking this plug-in live.

    Thank you very much for making such an easy to use and neat Prev/Next.

    Friday, May 28, 2004 at 6:38 am | Permalink
  2. ian wrote:

    Aha ! A simple mistake sorts out the extra ampersand – towards the end of the get_pagenum_link function, change the line after the if stristr to read:

    $qstr .= $querystring_separator.$page_querystring.$querystring_equal.$pagenum;

    It’s just a typo – there is a global definition for $querystring_equal above this point, but it’s never used.

    Friday, May 28, 2004 at 9:22 am | Permalink
  3. You’re right. I’ve corrected those two things, and actually another place where I’d placed $querystring_separator instead of $querystring_equal … thanks for the feedback. I’ll try to remember to test the search and nested categories next time [;-)]

    Man, with all that code in your comment, I’m rethinking my css formatting strategy [:-p]

    Friday, May 28, 2004 at 5:42 pm | Permalink
  4. David wrote:

    What change would be needed to make it instead of /page1 to end on /, like /page1/?

    TIA!

    Wednesday, June 2, 2004 at 11:37 am | Permalink
  5. Jerome wrote:

    Just to check…
    Where exactly do i put the codes concerning the styles?

    Thursday, June 3, 2004 at 7:17 am | Permalink
  6. David: I think the only change would be to the next to last line of code (line 86):

        return get_settings('home').$qstr."/";
    

    Jerome: the styles are CSS, so just put them in one of your current style sheets (eg: wp-styles.css)

    Thursday, June 3, 2004 at 10:53 pm | Permalink
  7. Tinuviel wrote:

    The plugin file is gone or isn’t working – can you bring it back?

    Wednesday, June 23, 2004 at 10:55 am | Permalink
  8. Sorry, I had a problem with it when I installed a nightly release, and I unthinkingly renamed the file instead of just disabling it temporarily [blush]

    Saturday, June 26, 2004 at 10:19 pm | Permalink

3 Trackbacks/Pingbacks

  1. [...] 085944494); Filed under: WordPress Plugins|@ 1:14 PM {} Huddled Masses has a newly written plugin giving your WP weblog a next and previous link [...]

  2. Blogging Pro - Blog News, Tools and Hacks on Sunday, May 30, 2004 at 2:19 pm

    WP Plugin: Previous and Next Links
    Huddled Masses has a newly written plugin giving your WP weblog a next and previous links to add anywhere on your index page. Supports search engine friendly URI’s too once you’ve added some mod_rewrite code to your .htaccess file.

  3. digressions on Monday, June 14, 2004 at 2:18 pm


    Huddled Masses…