I need some help with WordPress pagination.
I'm trying to paginate all posts in my categories pages. I've added at the end of the loop the paginate_links()
function but nothing appears in the front-end.
This is the code I use for displaying all "Design" posts:
<div role="main">
<h3 class="page-title">RECENT POSTS IN DESIGN</h3>
<div id="full-post-list" class="row between-xs margin-category-title">
<?php
if( is_category() ) {
$cat = get_query_var('cat');
$args = array(
'offset' => 1,
'posts_per_page' => 21,
'cat' => $cat,
'orderby' => 'date',
'order' => 'DESC',
'post_not_in' => get_option( 'sticky_posts' )
);
$query = new WP_Query($args);
if ( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
get_template_part( 'content', 'latest' );
}
}
}
?>
</div>
<div id="pagination">
<?php echo paginate_links(); ?>
</div>
</div>
The stranger thing is that if I type in the brower URL bar mysite.com/category/design/page/2 I don't see a 404 error but I display always the first page (and the pagination links are always hidden).
Any idea? What am I doing wrong?
Thank you!
You should pass parameters to this function like this, total page get from $query:
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
or defind global $wp_query, it will auto paginate with default params:
<?php
global $wp_query
if( is_category() ) {
$wp_query = new WP_Query($args);