Post Pic

WordPress tip : Better page navigation

With the previous_posts_link() and next_posts_link() functions, WordPress allows you to link previous and next pages. But if the reader reached out the last page, what about providing him a link to the blog archive instead of nothing?

This recipe is extremely easy to implement : open you index.php file and replace your current navigation by the following:

<?php if ($paged > 1) { ?>

	<div class="navigation">
		<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
		<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
	</div>

<?php } else { ?>

	<div class="navigation">
		<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
		<div class="alignright"><a href="http://perishablepress.com/press/archives/">Site Archives &raquo;</a></div>
	</div>

<?php } ?>

Thanks to Jeff Starr and Chris Coyier for this useful idea! Have you read their book? If no, I definitely recommend it (read review here)

9 Responses

Dec 22 2009 18:35

We can use WP Pagenavi for another solutions ;)

Dec 23 2009 14:17

It’s very good solution, I will change this recipe to make it do a link for the archive when it’s the last page too.

So Thanks…

Dec 29 2009 09:54

that’s a clever idea to keep those viewers who’ve reached the end of your site busy

Jan 19 2010 18:14

Excellent

Very nice recipe indeed, helped a lot!

Thanks

Jan 20 2010 00:23

I’m looking for exactly something like this, becouse i didn’t know how to change this.

thanks
Masa Cukrowa

Mar 24 2010 06:54

Hi,

I do not yet try to apply your recipe. For a long time, I have used WP Pagenavi. I really enjoy this WP plugin. Visitor can easily jump from some page to another one or from page 1 to the last page or the reverse.

Apr 13 2010 17:34

Yeah, It’s excellent. It will keep my visitor not going out of my blog. With better navigation, my visitor will move around just in my blog.
Thanks for your sharing.

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required