Custom WordPress Post Loop With Pagination via wp_pagenavi That Works!

Share

For a client project, I needed to display recent blog posts on a custom page. I used the standard WP_Query method of pulling a custom page loop (this post does a nice job of breaking down all the possible WordPress loops), but I had a problem. I wanted to use the WP-PageNavi plugin, but I couldn’t get it to work. I would click on the next page, and instead of loading page 2 of posts, it would show page one…even thought the URI clearly indicated page 2.

I found some advice that suggested resetting my permalinks, but that didn’t help. I also found a few blog posts that claimed to solve the problem, but none of them worked either. Fortunately, the trusty old WordPress CODEX had enough documentation of the WP_Query function that I was able to figure it out.

After hours of testing and searching, I arrived at the following solution:


 1,
    'paged' => $paged
  );

  $loop = new WP_Query($args);

while ($loop->have_posts()) : $loop->the_post(); ?>
		
     **DO LOOP STUFF**

 



The key is that the syntax for the get_query_var function changed as of WP 3.02 – the proper way to grab the page ID changed from get_query_var( 'paged' ) to get_query_var( 'page' ) – that’s paged to page.

Deleting the ‘d’ made it work.

10 Comments

  1. Jedi master of WordPress, you are.

  2. R. J. – LOL – thanks. I only know enough to be dangerous.

  3. Thanks for the help but the latest version must of fixed the error cause your code now works with paged instead of page.

  4. Exactly what I was looking for, the code is easy to understand and works like charm. Too bad I couldn’t come up with something like that myself. Thanks to you I’m back on track again, keep it up!

  5. Muchas gracias!! me funciona bien. Solo que mas bien yo lo dejé con paged y funciona bien.

  6. Hmmm, I can’t seem to get this to work. I’m using a woocommerce theme irresistible. Is there anything you could think of that would prevent this from working?

    When I click one of the pagination links the page reloads, the URL changes, but the content remains the same, as does the display of pagination.

    Any ideas?

  7. Matt – See Michael’s comment. It might be “paged” again.

  8. Thanks for the reply – it was actually just me being foolish and having another loop further up the page which interfered with it.

    Thanks anyway.
    Matt

  9. its showing the url that clicked properly..but not changing anything else.

  10. Thanks
    Matt

Comments are closed.

Share