1

I am trying to use two loops on my page, one to get the current page's content and so on, and another to get all of the posts of a specific category.

When I do that, all of the tags for the current page disappears.

Here is my code,

Loop one:

<?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
      <div class="content_wrapper"><?php the_content(); ?></div>
   <?php endwhile; ?>
<?php endif; ?>

Loop two:

<?php query_posts(array('category_name' => 'test')); ?>
<?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
      <div><?php the_excerpt(); ?></div>
   <?php endwhile; ?>
<?php endif; ?>

Any ideas?

Thanx in advance!

2
  • 1
    Whoops, forgot to reset the query after query_posts with wp_reset_query, after adding that it works now.
    – Odyss3us
    Mar 9, 2011 at 19:24
  • Well spotted though, i like how you spotted your error and corrected it(and posted it back here in the question), you've scored one in my book... ;)
    – t31os
    Mar 9, 2011 at 21:18

2 Answers 2

3

Sorry guys, stupid mistake, I forgot to reset the second loop using wp_reset_query, here is the working code,

<?php query_posts(array('category_name' => 'test')); ?>
<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <div><?php the_excerpt(); ?></div>
    <?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>

Thanx anyways though!

0

Odyss3us, I have read a lot about multiple queries lately and it seems there is a lot of bad practice floating around about the best way to include multiple loops in a single page. Take a look at this for reference on why you should be using WP_Query instead of query_posts. There is less risk of causing havoc using WP_Query, which query_post calls anyway. Hope this sheds some light for other readers as well.

WP_Query Functions

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.