0

From what I understand, if the page is the front page (using front-page.php) you would use get_query_var('page') and for other pages you would use get_query_var('paged'). I have a condition for this and the pagination works to an extent. If I click on "Next" it shows the next set of records however on the home page the pagination itself doesn't change ie: on page 2, the number 2 isn't highlighted and the 'previous' button isn't present like on other pages.

if( is_front_page() ) {
    $paged = (get_query_var('page')) ? get_query_var('page') : 1;
}else {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
}

$args = array(
    'posts_per_page' => 12,
    'post_status' => 'publish',
    'paged' => $paged,
);

$all_posts = new WP_Query($args);


 <?php while ($all_posts->have_posts() ): $all_posts->the_post(); ?>
    display post title, date etc. here
 <?php endwhile; wp_reset_postdata();?>
 
<div class="pagination">
    <?php
$big = 999999999;
echo paginate_links(
    array(
        'prev_text' => __( 'Previous' ),
        'next_text' => __( 'Next' ),
        'base' => str_replace($big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
        'format' => '?paged=%#%',
        'current' => max(
            1,
            get_query_var('paged')
        ),
        'total' => $all_posts->max_num_pages
    )
);
?>
</div>

1 Answer 1

1

Yes, you're correct that on the static front page, we use get_query_var('page') to get the current page number.

And actually, the issue in question ("the number 2 isn't highlighted") happened because you used get_query_var('paged') (note the paged) with the current arg for paginate_links(), which means you're passing the wrong number.

'current' => max(
    1,
    get_query_var('paged')
),

So all you need to do is replace that with 'current' => $paged, and the problem will be gone.

1
  • 1
    Ah, of course! Thanks so much for that :)
    – Waterfall
    Feb 21, 2022 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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