最新消息: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)

Query Post interferes with Pagination

matteradmin5PV0评论

Here is my situation, if you look at the following page: /, you will see that the pagination is not working. After researching the problem, I found the problem within the following code:

    foreach ($categories as $cat) {
       $cat_id = $cat->term_id;

       if($cat->name != 'Uncategorized' && $cat->name != 'Hero') :
          $catURL = get_category_link($cat_id);
          echo "<div class='side-cat'><button>".$cat->name."</button><ul>";
          query_posts("cat=$cat_id&posts_per_page=5");
            if (have_posts()) : while (have_posts()) : the_post(); ?>
             <li><a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
              <?php endwhile; endif; ?>
             <li><a href="<?php echo $catURL; ?>" title="Read All <?php echo $cat->name; ?> Stories">More. . .</a></li>
                    </ul></div>
               <?php endif;  } ?>

The main culprit was the query_posts. Is there an alternative to the query posts so it doesn't interfere with the pagination?

I'm using Wordpress 5.1

Thank you, Kevin Davis

Here is my situation, if you look at the following page: http://staging.cancerwellness/mind-body/cancerversary/, you will see that the pagination is not working. After researching the problem, I found the problem within the following code:

    foreach ($categories as $cat) {
       $cat_id = $cat->term_id;

       if($cat->name != 'Uncategorized' && $cat->name != 'Hero') :
          $catURL = get_category_link($cat_id);
          echo "<div class='side-cat'><button>".$cat->name."</button><ul>";
          query_posts("cat=$cat_id&posts_per_page=5");
            if (have_posts()) : while (have_posts()) : the_post(); ?>
             <li><a href="<?php the_permalink();?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
              <?php endwhile; endif; ?>
             <li><a href="<?php echo $catURL; ?>" title="Read All <?php echo $cat->name; ?> Stories">More. . .</a></li>
                    </ul></div>
               <?php endif;  } ?>

The main culprit was the query_posts. Is there an alternative to the query posts so it doesn't interfere with the pagination?

I'm using Wordpress 5.1

Thank you, Kevin Davis

Share Improve this question asked Mar 29, 2019 at 15:31 Kevin DavisKevin Davis 1071 gold badge2 silver badges6 bronze badges 1
  • Query_posts is specifically designed to alter the main page query. For additional queries, you can create addition WP_Query objects and operate on them instead. – tmdesigned Commented Mar 29, 2019 at 16:44
Add a comment  | 

1 Answer 1

Reset to default 0

query_posts() is going to alter the main query, and it doesn't sound like you want that. I would recommended using the WP_Query object to get the other posts.

That said, if you must use query_posts(), call wp_reset_query() at the end of your foreach to reset your main query back to the original one.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far