I’m trying to list the posts from the category ‘alps’. The problem is if I define ‘posts_per_page’=> -1, that works fine and I get the complete list, but if I ask for a number of posts I just get the same posts repeating page after page. Here’s my loop-alps.php file.
<?php
$args = array(
'order' => 'asc',
'order_by' => 'title',
'posts_per_page'=> 5,
'category_name'=> 'alps'
);
$wp_query = new WP_Query($args);
if($wp_query->have_posts()): while($wp_query->have_posts()): $wp_query->the_post();
echo '<h1>' .get_the_title() .'</h1>';
the_post_thumbnail();
endwhile;
endif;
?>
<div class="navigation">
<?php if(function_exists('tw_pagination')) tw_pagination($the_query); ?>
</div>
According to WP documentation (Codex) it seems that you should use the
paged
parameter in order to make it work correctly.get_query_var( 'paged' )
– this function basically look for a GET variable in the URL, ‘?paged=X’ if i’m not mistake. So make sure that by clicking on the pagination links you can see that this parameter is being added to the URL and is being changed accordingly.Source: https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
Just add
'paged' => get_query_var( 'paged' ),
like this, adjust it to your own