$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>paginate links - How to use multiple paginate_links() in my index.php?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far