0
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array( 'posts_per_page' => 6, 'offset'=> 0, 'category' => 1, 'paged' => $paged );

$custom_query = new WP_Query( $args );
while($custom_query->have_posts()) :
    $custom_query->the_post();
?>

My Html code

<?php endwhile; ?>
<?php
    previous_posts_link('&laquo; Newer posts');
    next_posts_link( 'Older posts &raquo;', $the_query->max_num_pages );
    wp_reset_postdata();
?>

What's wrong?

0

2 Answers 2

0

You're having pagination issues because you're using a custom query and both previous_posts_link and next_posts_link use the default query to calculate and display the pagination.

This problem is explained in-depth in both this and this answers to similar questions.

If the only reason for you to be using a custom query is for defining the post count on pagination, then I strongly suggest you use the default query and apply this answer.

0

Use the function https://codex.wordpress.org/Function_Reference/paginate_links

$args = array(
    'base'         => '%_%',
    'format'       => '?page=%#%',
    'total'        => 1,
    'current'      => 0,
    'show_all'     => False,
    'end_size'     => 1,
    'mid_size'     => 2,
    'prev_next'    => True,
    'prev_text'    => __('« Previous'),
    'next_text'    => __('Next »'),
    'type'         => 'plain',
    'add_args'     => False,
    'add_fragment' => '',
    'before_page_number' => '',
    'after_page_number'  => ''
); 

echo paginate_links( $args );

Not the answer you're looking for? Browse other questions tagged or ask your own question.