Paginated pages are showing correct content but pagination links are not

Static pages doesn’t work with paged query variable, they need the page variable.

This is the reason why your second code block makes the query work: it uses the page var when available.

However, your paginated links code always use paged:

...
'format' => '?paged=%#%',
...

So you are sending paged query var but looking for page var.

Solution is to make your format argument send page query var if used from a static page.

if ( $latest_query->max_num_pages > 1 ) :

    $var = is_page() ? 'page' : 'paged'; // <-- choose var here   

    $big = 999999999;
    echo '<div class="pagination">';
    echo paginate_links( array(
        'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),

        'format' => '?' . $var . '=%#%', // <-- use var here  

        'current' => max( 1, get_query_var($var) ), // <-- use var here 
        'total' => $latest_query->max_num_pages
    ) );
    echo '</div>';
endif;

Untested.

Leave a Comment