0

I'm trying to create pagination to the custom wp_query here is my code:

<?php
    $btpgid=get_queried_object_id();
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $args = array( 
        'cat' => 1,
        'posts_per_page' => 6, 
        'paged' => $paged,
        'post_type' => 'post' 
    );
    $postslist = new WP_Query( $args );

    if ( $postslist->have_posts() ) :
        while ( $postslist->have_posts() ) : $postslist->the_post(); ?>
                                            
                                            
            <div class="col-lg-6 right-padding m-t-50">
                <a href="<?php echo get_permalink(); ?> " title="" class="case-box blog-box" data-aos="flip-left">
                    <span class="green-filter case-image-holder">
                        <?php echo get_the_post_thumbnail(); ?> 
                    </span>
                    <span class="content">
                        <span class="blog-box-title">
                           <?php echo the_title(); ?>
                        </span>
                    </span>
                    <p class="section-p"><?php echo get_the_excerpt(); ?></p>
                </a>
            </div>

        <?php endwhile;  ?>
    <div class="pagination d-flex align-items-center justify-content-center w-100 m-t-50">
        <?php 
            next_posts_link( '< Starsze posty', $postslist->max_num_pages );
            previous_posts_link( 'Nowsze posty >' ); 
            wp_reset_postdata(); ?>
    </div>
<?php endif; ?>

It all works fine when i'm using only "previous" and "next" site pagination like so:

<?php 
    next_posts_link( '< Starsze posty', $postslist->max_num_pages );
    previous_posts_link( 'Nowsze posty >' ); 
    wp_reset_postdata(); 
?>

But when i'm trying to use numbered pagination - it displays nothing - like that:

<?php the_posts_pagination( array(
    'mid_size'  => 2,
    'prev_text' => __( 'Back', 'textdomain' ),
    'next_text' => __( 'Onward', 'textdomain' ),
) ); ?>

Do i have some mistakes in the code? what am i doing wrong?

4
  • 1
    Welcome to wpse! take a look at Pagination when using wp_query?. It looks like you are missing some properties in the array thats being passed to the the_posts_pagination. You can use the complete answer in the link or just copy the array, just change the properties accordingly. Oct 14, 2021 at 8:57
  • Thank you - works like a charm! Oct 14, 2021 at 9:09
  • 1
    Consider added your working code as a answer so future users that encounter the same problem can your answer to help them out. Oct 14, 2021 at 9:10
  • Sure already did that - thanks a lot Oct 14, 2021 at 9:11

1 Answer 1

1

Thanks to @Buttered_Toast i've managed to get it work by changing:

<?php the_posts_pagination( array(
    'mid_size'  => 2,
    'prev_text' => __( 'Back', 'textdomain' ),
    'next_text' => __( 'Onward', 'textdomain' ),
) ); ?>

to:

<?php  echo paginate_links( array(
    'base'         => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
    'total'        => $postslist->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'    => true,
    'prev_text'    => sprintf(  __( '<', 'text-domain' ) ),
    'next_text'    => sprintf(  __( '>', 'text-domain' ) ),
    'add_args'     => false,
    'add_fragment' => '',
    ) );
wp_reset_postdata(); ?>

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.