Query Post interferes with Pagination

Here is my situation, if you look at the following page: , you will see that the pagination is not working. After rese

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745645323a4637934.html

相关推荐

  • Query Post interferes with Pagination

    Here is my situation, if you look at the following page: , you will see that the pagination is not working. After rese

    22天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信