WP_query pagination on frontpage

Question

I’m trying to make a pagination for a WP_Query on a frontpage using the paginate_links() function and following the codex (https://codex.wordpress.org/Pagination#Static_Front_Page).
The pagination displays well after 3 posts (as I request it in the new WP_Query parameter) and when clicking on the ‘2’ link, it leads to the /page/2 with the next three posts correctly. Yay!

The problem I’m struggling with is that on the page 2, the pagination seems stuck on the page 1 aspect : ‘1’ is not clickable and ‘2’ is clickable which lead to the same page we are.

Please find my code below :

    <?php

    if ( get_query_var( 'paged' ) ) { 
        $paged = get_query_var( 'paged' ); 
    } elseif ( get_query_var( 'page' ) ) { 
        $paged = get_query_var( 'page' ); 
    } else { $paged = 1; }

    $query = new WP_Query('posts_per_page=3&paged=' . $paged);

    // Check that we have query results.
    if ( $query->have_posts() ) :
      // Start looping over the query results.
      while ( $query->have_posts() ) :
        $query->the_post(); ?>
        <article class="post-content">
            <div class="post_meta flex-container flex-space-between">
                <h2 class="post_title"><?php the_title(); ?></h2>
                <?php the_category(); ?>
            </div>
                    <?php the_content(); ?>
                </article>
        <?php
      endwhile;

      echo paginate_links( array(
        'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
        'total'        => $query->max_num_pages,
        'current'      => max( 1, get_query_var( 'paged' ) ),
        'format'       => '?paged=%#%',
        'show_all'     => false,
        'type'         => 'plain',
        'end_size'     => 2,
        'mid_size'     => 1,
        'prev_next'    => false,
        'add_args'     => false,
        'add_fragment' => '',
     ) );

     wp_reset_postdata();

    endif;
    ?>

What do I miss?
Thanks by advance for your coming help

0
jrmy_o 4 months 0 Answers 14 views 0

Leave an answer