How to Display The Next Post/ Previous Post In The Same Category or Tag
A lot guys ask me how to display the next post and previous post in the WordPress single.php template, also I have no idea, and I have searched for a long time and found a solution, which can help you display the next post in the same category. (The WordPress’ default next/previous functions seems useless these days, especially for the news, magazine WordPress themes!)
In fact, the WordPress has it’s own build in function, which can help you display the next post in the same category, you can check it out here.
Here is the code, copy and paste to your single.php
<?php next_post_link('%link', 'Next post in category', TRUE); ?> <?php previous_post_link('%link', 'Previous in category', TRUE); ?>
However, this function can’t work if you assign one post to multiple categories! Is there any ways to solve this problem? And most of the time, when you click one tag, then your site may list a lot posts under one tag, usually you need to click one link, then click another link, right? How can we just click the next link then we can see the next post?
What if the user came to that post from a tag page or from an author page? Wouldn’t it make more sense to display previous / next posts from that particular set?
O.K, here is the solution for these headache problems!
Here is an awesome plugin for you to use: that’s Smarter Navigation
Whenever a visitor goes to an archive page (category, tag, date, author, search etc.), the plugin notes which archive it is in a browser cookie, then you can click the next post link to view the next post under the same category or tag or search results!!!
How to use it?
Copy and paste the following php code to your single.php file
<?php previous_post_smart(); ?> <?php next_post_smart(); ?>
Really cool, right? Download it now!