arens
arens

Reputation: 1

Pagination only displaying the first page

The blog page for my wordpress site has the various pages on the bottom of the page, but will only display the first page of results despite which number page you click. A sample of my code is below, let me know if you need more.

if(!is_front_page())
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
else
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     $args = array('offset'=> 0, 'paged'=>$paged, 'posts_per_page'=>$posts_per_page);
     $wp_query = new WP_Query($args);

if(have_posts()) : 
     while(have_posts()) : the_post();

Upvotes: 0

Views: 832

Answers (1)

JetLewis
JetLewis

Reputation: 11

You are using the main loop to query your posts and not your custom query, plus it's not wise to use the variable name $wp_query it could be conflictual with the global var $wp_query, try changing:

$wp_query = new WP_Query($args);

if(have_posts()) : 
     while(have_posts()) : the_post();

to

$query = new WP_Query($args);

if($query->have_posts()) : 
     while($query->have_posts()) : $query->the_post();

Now the reason why the pagination is not working is also likely because you use the argument "offset" which breaks the pagination:

offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination (Click here for a workaround). The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used. source: https://codex.wordpress.org/Class_Reference/WP_Query

It is irrelevant tu use offset: 0 (default) in your situation.

Upvotes: 0

Related Questions