WordPress next and previous post link on index.php
In two recent projects I have been faced with a problem. The problem is actually quite simple but I couldn’t figure out a solution for it, so I decided to call in the big guns.
I’m sure you are all aware of the <?php previous_post_link('%link','Previous Article') ?> and <?php next_post_link('%link','Next Article') ?> wordpress links that you can use on your single.php pages when you want to offer a form of navigation between posts.
The problem
In these projects I’ve set index.php to display the most recent post but I wanted the ability to use the next and previous post link, to help users cycle through other blog posts. However because index.php is a page and not a post I wasn’t able to use the regular method mentioned above. Therefore the only possible solution for the user to navigate through posts using the wordpress navigation was to click on the post title to enter the post page and then click through the posts using the navigational link but as I’m sure you will all agree isn’t very logical.
The solution
Chris got back to me very quickly with his solution.
<?php query_posts('posts_per_page=1&offset=1'); the_post(); ?>
<a href="<?php the_permalink() ?>">Previous Post</a>
<?php wp_reset_query(); ?>
Chris then went on to explain how the above code works
WP_Query is just another form of query_posts that is a little more advanced and by nature doesn’t interfere with other loops. We could have used that for this too, but I just like query_posts better. And if there was a regular loop, that’s why I put that reset_query() call, so it wouldn’t mess with it.
The Kudos
I just want to say thank you to Chris once again for taking time out of his day to help me with this problem that has troubled me for a good few weeks now. If you haven’t heard of Chris before then I’m assuming you must have been living under a rock. You can check out his great multipurpose site called CSS-Tricks or you can read his WordPress dedicated site called Digging into WordPress to receive a series of great hints, tips and tutorials.
-
Who I am
My name is Jack Osborne and I am a Glasgow–based designer and writer. You should follow me on Twitter.
-
What I do
I am a front-end developer with proficiency in; accessibility, design, semantics, usability & web standards.
-
Where I do it
I am currently working freelance and I am available for hire. Feel free to get in touch with me if you fancy a chat.
Thats a pretty neat snippet of code
Although it is kinda limited to designs that only show one post on the index page (which I don’t do often…).
I’m sure I’ll end up using this in projects in the future though! Thanks (to you and Chris)!
Actually Nick, if you set the number of posts per page in the admin panel (the exact setting eludes me) you can actually pull that value – it’s a global variable with a name I also can’t remember right now – and then you could incorporate that into the query.
That way this function can actually work on a variety of layouts without hardcoding the “1″ into it.
handy snippet for any wordpress user.
good work you guys
Nice to see that Chris is there to help the community with questions. Nice simple way to get the job done. Thanks for the good post!
@chris totally agree, he’s a credit to the community.
Thanks, man. You just saved my sanity!