I have my main blog, which lists all posts.
I also have a category page that lists only posts from 1 category.
Main Blog
- Post #1
- Post #2
- Post #3
- Post #4
- Post #5
Category A Page
- Post #1
- Post #3
- Post #4
Category B Page
- Post #1
- Post #3
- Post #5
If a user clicks to look at a post, and then uses the default next/prev link functions, there is no way for WordPress to know which post should be next.
For example, if the user is looking at Post #3, should the next post be #4 or #5? It all depends on where the user came from.
So I wrote the following code to answer this problem and thought I'd share it.
NOTE: I know that this is not the most efficient way to do this, especially if you have thousands of posts. I'd be interested in seeing better ways to get the next post.
Add the following to your functions.php file:
And then on any category.php or page where you show several blog posts (like a "related posts" section), run this function:
Then on your single.php you can use the following functions.
The '-24, 10' is a parameter that allows you to exclude, or specifically include categories via comma separated ID's.