Working on a few hundred WordPress projects within about 13 years, you come across a few things that you can explain why they just don’t work and right away you notice the culprit issue which in most cases some developer just seem to forget about.
In this article we are going to take a quick look at WordPress function for displaying Previous and Next links on your post indexing pages or article listing pages. If properly implemented everything should work out of the box. However, we noticed that a lot of times the out-of-the-box is not working for two main reasons. Reason one, is that previous development of your theme is done incorrectly, leaving WordPress query loops and open php code just opened. Reason two, your loop is missing an extra string that allows this out of the box function to work or WordPress to recognize that we moved on from page one to page two, three and so on. In other words, the effect we get is that our WordPress article list page is showing same posts on page 1, page 2 and even page 10, when it should be showing us 10 different articles on page 1, and 10 different articles on page 2, and so on.
So let’s take a quick look at what possibly might be the solution to the problem, if one exists.
Number one problem, our previous and next links work but either don’t show posts at all or show same posts no matter what page you are on. The problem we’ve found in WordPress template building with previous and next post links is that when you add the code tag <?php next_posts_link(”) ?> or <?php previous_posts_link(”) ?> to your index.php page or any page-.php file, is that it still will work creating the link and paginate, but no matter which page you are on (https://www.domain.com/page/2 or https://www.domain.com/page/10) it is still showing you the same exact articles. No matter which page you last, if you have setup for WordPress to query and display 10 articles per page, each and every pagination (1, 2, 3, or even 20) will still show that same exact 10 articles.
Our fix is very simple. It’s one of those things that for some reason many developers over look. In most cases, its a learning curve for the very new at WordPress. What most are forgetting, is to let the post loop in WordPress know which page we are at. Think of it this way, till now, since you’re having the issue, WordPress processing and listing your posts as articles only thought you’ve been on page 1 with 10 articles on it. That’s all it thought you were doing, even if you were on page 10. So let’s help it, learn what page we are truly on. To do that, you have to locate the following like which sits some where near the index.php file.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
Now let’s go and change that just a big. Remember, we’re just letting WordPress index.php file know, that when we click on previous or next link, we’re switching pages thus new articles next in line should show. So the correct line should go something like this;
<?php query_posts( $query_string . '&pagename=&page_id=' ); if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
That’s it! The above “query_posts( $query_string . ‘&pagename=&page_id=’);” entry line fixes our issue where next and previous post links stopped working.
Now that wasn’t so hard was it? If you still can’t get it working, even with the suggest line above then there might be something even deeper going on wrong with your theme. Don’t hesitate to contact us directly, as we are always happy to help fixing WordPress issues or contact your theme developer.