paginate links - How to use multiple paginate_links() in my index.php?

admin2025-06-02  2

I've created two divs which displays posts from two different categories. The paginate_links() function works only for the first div and not for the second.

I tried @Boone Gorges answer: Multiple WP_Query loops with Pagination

Here's my code:

<div>
<?php
      $paged1= isset($_GET['paged1'])?(int)$_GET['paged1']:1;
      $paged2= isset($_GET['paged2'])?(int)$_GET['paged2']:1;
      $newsposts=new WP_Query(array(
        'post_type'=>'post',
        'category_name'=>'news',
        'paged'=>$paged1,
        'posts_per_page'=>3
      ));


          while($newsposts->have_posts())
          {
            $newsposts->the_post();
    ?>
    //some html code
      <?php } 
        $pag_args1= array(
          'format'  => '?paged1=%#%',
          'current' => $paged1,
          'total' => $newsposts->max_num_pages,
          'add_args' => array( 'paged2' => $paged2 )
      );
      echo paginate_links( $pag_args1 );



       ?>

  </div>


//second div
<div>

    <?php

      $eventsposts= new WP_Query(array(
        'post_type'=>'post',
        'category_name'=>'events',
        'paged'=>$paged2,
        'post_per_page'=>3
      ));


          while($eventsposts->have_posts())
          {
            $eventsposts->the_post();
    ?>
//some html code
      <?php } 
        $pag_args2 = array(
          'format'  => '?paged2=%#%',
          'current' => $paged2,
          'total'   => $eventsposts->max_num_pages,
          'add_args' => array( 'paged1' => $paged1 )
      );

      echo paginate_links( $pag_args2 );


      ?>
  </div>

The links are displayed and works only for the first div; the second div does not even display its links.

The URL when clicked on the next page of the first div looks like this: http://localhost/wordpress/?paged1=2&paged2=1

I tried changing paged2=2.. this does not work either.

Thanks for your help.

I've created two divs which displays posts from two different categories. The paginate_links() function works only for the first div and not for the second.

I tried @Boone Gorges answer: Multiple WP_Query loops with Pagination

Here's my code:

<div>
<?php
      $paged1= isset($_GET['paged1'])?(int)$_GET['paged1']:1;
      $paged2= isset($_GET['paged2'])?(int)$_GET['paged2']:1;
      $newsposts=new WP_Query(array(
        'post_type'=>'post',
        'category_name'=>'news',
        'paged'=>$paged1,
        'posts_per_page'=>3
      ));


          while($newsposts->have_posts())
          {
            $newsposts->the_post();
    ?>
    //some html code
      <?php } 
        $pag_args1= array(
          'format'  => '?paged1=%#%',
          'current' => $paged1,
          'total' => $newsposts->max_num_pages,
          'add_args' => array( 'paged2' => $paged2 )
      );
      echo paginate_links( $pag_args1 );



       ?>

  </div>


//second div
<div>

    <?php

      $eventsposts= new WP_Query(array(
        'post_type'=>'post',
        'category_name'=>'events',
        'paged'=>$paged2,
        'post_per_page'=>3
      ));


          while($eventsposts->have_posts())
          {
            $eventsposts->the_post();
    ?>
//some html code
      <?php } 
        $pag_args2 = array(
          'format'  => '?paged2=%#%',
          'current' => $paged2,
          'total'   => $eventsposts->max_num_pages,
          'add_args' => array( 'paged1' => $paged1 )
      );

      echo paginate_links( $pag_args2 );


      ?>
  </div>

The links are displayed and works only for the first div; the second div does not even display its links.

The URL when clicked on the next page of the first div looks like this: http://localhost/wordpress/?paged1=2&paged2=1

I tried changing paged2=2.. this does not work either.

Thanks for your help.

Share Improve this question edited Feb 20, 2019 at 7:47 Jacob Peattie 44.3k10 gold badges50 silver badges64 bronze badges asked Feb 20, 2019 at 7:18 N3moN3mo 115 bronze badges 3
  • Try wp_reset_query() after your first call to paginate_links(). wordpress.stackexchange/questions/144343/… – WebElaine Commented Feb 20, 2019 at 14:54
  • Tried it, Did not work. – N3mo Commented Feb 24, 2019 at 15:25
  • Even the post_per_page argument doesn't seem to work in the second div. The extra posts just overflow. – N3mo Commented Feb 24, 2019 at 15:26
Add a comment  | 

1 Answer 1

Reset to default 0

After a week of getting stuck, found out the solution. I had to put an extra argument 'showposts' for my $eventposts and $newsposts.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748861730a314334.html

最新回复(0)