A better way to manage page navigation in WordPress

I’ve seen this snippet of code flying around the Internet since yesterday and I’ve finally got a chance to blog about it.

Page navigation can become very tricky with wordpress especially when you’re doing custom query_posts() calls, but even the basics can be tricky and it’s not always best to simply use the built in next_posts_link() and previous_posts_link() to fulfill your requirements.

Here’s some code, which proves a lot better:

<?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://www.yourlink.com/archives/>Site Archives &raquo;</a></div>
</div>

<?php } ?>

You can read a full description here to get a better understanding.

Christopher is the founder of iMod - Most of his time is spent building websites and pushing the limits with Search Engine Optimization. You can follow him on Twitter @ChristopherM

Enjoy this post? Please tell a friend:

Comments are closed.