admin管理员组

文章数量:1026989

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.

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.

本文标签: Query Post interferes with Pagination