Winnipeg Photographer

WordPress – How to fix next_posts_link() doesn’t work

I first off want to thank Chris Coyier of! If you’ve never seen or heard of Chris you don’t know what you are missing. My answer to this question came from watching the 31:14 minute video on css-tricks called “the WordPress Loop”.

The most likely reason why next_post_link() doesn’t work in your theme is mostly do to how you setup the loop. The part of the loop that is most likely not setup is the “paging” part of it. Because paging isn’t setup you display the same content OVER and OVER each time you click on the “Newer Entries”. By setting up paging you are telling WordPress to display “X” amount of posts per page. The “offset” tells WordPress where to start on the next page.

I will post a screenshot of some code and go over what you need to do:

Wordpress - How to fix next_posts_link() doesn’t work

We need to use the “query_posts()” function (Line 15). In order to get the query that you want you should make a few variables first.

  • On line 12 we create a variable called “ppp” (Posts Per Page) and give it a number.
  • On line 13 we create the offset variable. This will help with a term called “Paging”.

Now that we have the variables setup we are going to create a custom WordPress Loop. To do this we need to make a new query.

  • On line 15 I show you how to make a custom query using the variables we created earlier.

The query basically says: Go out and get me all the posts. I know I am going to be displaying “2” posts per page. Each time I go and click on “Newer Entries” start at the “offset” and then display another set of posts equaling whatever you set the “ppp” to be.

You might be asking yourself is there another way of doing this? Actually there is if you are using this as the only loop on a page and you want just spit out the blog entries.

Create a page template file and name it “pagetemplate.php” and put it into your theme directory. If you are using the same info in the screenshot the template name you will be able to select in the next step is going to be called “Playing with Loop”. *NOTE* – You won’t need lines 12 to 16!

Create a new page in WordPress and open it. I’m going to call my page “Blog”. On the right hand side under “Page Attributes” you see “Template”. Select “Playing with Loop” and hit update. You must now go and make some changes in the WP Admin area. Go to “settings” and “reading” and select the page that you want to be the default “Posts” page. Since as I previously mentioned I called my page “Blog” I am going to select “Blog” and voila. WordPress now knows that since this page is going to be displaying the posts, the paging will already be setup.

Wordpress - How to fix next_posts_link() doesn’t work

Leave a Reply