1

Pagination doesn't work on a single page of WP.

In the code below, everything looks great except the pagination shows the same contents on the next page.

I tried every possible solution I found on the internet, but none of them worked. I'm relatively new to WP and php, so if you could pinpoint the code that might be wrong, that'd be really helpful.

          <?php
          $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' 
         ) : 1;
          $args = array(
            'posts_per_page' => 3,
            'paged'          => $paged,
            'post_type' => 'book',
            'offset'   => 1,

            'tax_query' => array( 
              array(
                'taxonomy' => 'news_cat', 
                'field' => 'slug', 
                'terms' => array( 'disney' ) 
              ),
            ),   
          );?>

          <?php $the_query = new WP_Query( $args ); 
          ?>


        <?php if ($the_query->have_posts()) ?>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>  

        <a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >                                                         
        <li>
          <div>
            <figure><img src="<?php the_field('thumb'); ?>" alt="">           
          </figure>
          </div>
          <span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
          <p><?php the_title(); ?></p>
        </li>
        </a>

    <?php endwhile; ?>


    <?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
    $args = array (
        'prev_text' => '',
        'next_text' => '',
        'show_all'  => false,
        'mid_size'  => 1,
        'type'      => 'list'
    );
    the_posts_pagination($args);?>   

I expect the pagination to work correctly instead of showing the same contents on all pages.

As you see, I want to make the pagination work only on the posts with "disney" slug.

gwen
  • 95
  • 8

1 Answers1

1

You can replace the code instead of your old code

<?php
          $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' 
         ) : 1;
          $args = array(
            'posts_per_page' => 3,
            'paged'          => $paged,
            'post_type' => 'book',
            'offset'   => 1            
            ),   
          );?>

          <?php $the_query = new WP_Query( $args ); 
          ?>


        <?php if ($the_query->have_posts()) ?>
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>  

        <a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >                                                         
        <li>
          <div>
            <figure><img src="<?php the_field('thumb'); ?>" alt="">           
          </figure>
          </div>
          <span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
          <p><?php the_title(); ?></p>
        </li>
        </a>

    <?php endwhile; ?>


    <?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
    $args = array (
        'prev_text' => '',
        'next_text' => '',
        'show_all'  => false,
        'mid_size'  => 1,
        'type'      => 'list'
    );
    the_posts_pagination($args);?>   

Now, you can check it that pagination is working or not.

Priyanka Modi
  • 1,594
  • 1
  • 8
  • 14
  • 1
    Thank you for your comment, but the pagination still doesn't work. It shows the same contents on all pages. Also, I want only the posts with "disney" slug shown. Any advice? – gwen Mar 26 '19 at 22:09