WordPress WP-PageNavi plugin not working. How to fix?
Everything is right and… (again) not
Hi again, I have been fighting with this plugin lately: wp-pagenavi. It should give me the pagination for my blog — but not. I have installed it correctly and trying to get it working with “WP_Query();”. I don’t get it, my loop is correct but WP-PageNavi doesn’t work. –Oh s*it sherlock, I have solved the problem for you.
How to paginate with WP-PageNavi?
Okay, the solution is a bit annoying. Couple of working hours wasted again into oblivion. This is the better way of doing things. Enough talking — let’s do this.
Solution for the “WP_Query()”:
$q = new WP_Query( array( 'posts_per_page' => 5, 'paged' => get_query_var('page'))); while ( $q -> have_posts()) : $q -> the_post(); //stuff here endwhile; wp_pagenavi( array( 'query' => $q )); // * wp_reset_postdata();
* You need to send the query for “wp_pagenavi()” -function:
wp_pagenavi( array( 'query' => $q ));
That’s it for the “WP_Query()”. It should work.
The old way — “query_posts()”
Okay, the old way also works just fine with WP-PageNavi plugin. I would say it’s more simplier that the WP_Query() — but according to the developer “the WP_Query() is better, because it has less side-effects”. I really don’t know what those “side effects” are, but it doesn’t matter.
For now, let’s focus on the old way: query_posts() right usage:
query_posts( array( 'cat' => 1, 'paged' => get_query_var('page') ) ); while ( have_posts()) : the_post(); //stuff here endwhile; wp_pagenavi(); wp_reset_postdata();
It should work just fine. Let me know it this helped you out. Here you can find the developer’s guide for the correct usage of WP-PageNavi plugin.