Title: Multiple Loops
Last modified: August 18, 2016

---

# Multiple Loops

 *  Resolved [Ryan Fitzer](https://wordpress.org/support/users/ryanfitzer/)
 * (@ryanfitzer)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/multiple-loops-1/)
 * I’m trying to have two Loops on my index.php page with each one only displaying
   one category. Can anyone give me an idea where I’m going wrong. Here’s the code:
 * `<?php query_posts('cat=1&showposts=2'); ?>`
 *  <?php if (have_posts()) : ?>
 *  <?php while (have_posts()) : the_post(); ?>
 *  <div class="post" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink()?
   >" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title();?
   ></a></h3>
 *  <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;');?
   > </div>
 *  </div>
 *  <?php endwhile; ?>
 *  <?php query_posts('cat=2&showposts=2'); ?>
 *  <?php if (have_posts()) : ?>
 *  <?php while (have_posts()) : the_post(); ?>
 *  <div class="post" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink()?
   >" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title();?
   ></a></h3>
 *  <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;');?
   > </div>
 *  </div>
 *  <?php endwhile; ?>
 *  <?php else : ?>
 *  <h3 class="center">Not Found</h3>
    <p class="center">Sorry, but you are looking
   for something that isn't here.</p>
 *  <?php endif; ?>
 * This gives me a parse error “unexpected $”. I’ve read about multiple loops in
   the codex and on the forums but I’m still not getting it. Any help or advise 
   would be appreciated. Thanks.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [Ryan Fitzer](https://wordpress.org/support/users/ryanfitzer/)
 * (@ryanfitzer)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/multiple-loops-1/#post-407626)
 * It figures. Right after I posted this I found my answer. Here is where I got 
   it:
 * [http://comox.textdrive.com/pipermail/hackers/2005-January/003578.html](http://comox.textdrive.com/pipermail/hackers/2005-January/003578.html)
 * And here is how the loop is formatted:
 * `// Show only posts in category 1 "current"
    <?php query_posts('cat=1&showposts
   =2'); ?>
 *  <?php if (have_posts()) : ?>
 *  <?php while (have_posts()) : the_post(); ?>
 *  <div class="current" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink()?
   >" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title();?
   ></a></h3>
 *  <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;');?
   > </div>
 *  </div>
 *  <?php endwhile; ?>
 *  // Show only posts in category 2 "news"
    <? $my_query = new WP_Query('cat2&showposts
   =2'); ?>
 *  <? while ($my_query->have_posts()) : $my_query->the_post(); ?>
 *  <div class="news" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink()?
   >" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title();?
   ></a></h3>
 *  <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;');?
   > </div>
 *  </div>
 *  <? endwhile; ?>
 *  <?php else : ?>
 *  <h3 class="center">Not Found</h3>
    <p class="center">Sorry, but you are looking
   for something that isn't here.</p>
 *  <?php endif; ?>
 * This is altered to only show the post title and post content. You’ll have to 
   put all the post meta back in if you want it to be back to the default.
 *  Thread Starter [Ryan Fitzer](https://wordpress.org/support/users/ryanfitzer/)
 * (@ryanfitzer)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/multiple-loops-1/#post-407634)
 * Sorry. Scratch that last block of code. I forgot to put “php” in a few places.
   Here’s the correct version:
 * `<?php query_posts('cat=1&showposts=2'); ?>`
 *  <?php if (have_posts()) : ?>
 *  <?php while (have_posts()) : the_post(); ?>
 *  <div class="post" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink()?
   >" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title();?
   ></a></h3>
 *  <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;');?
   > </div>
 *  </div>
 *  <?php endwhile; ?>
 *  </div>
 *  <div class="news">
 *  <h2>News and Events</h2>
 *  <?php $my_query = new WP_Query('cat2&showposts=2'); ?>
 *  <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
 *  <div class="post" id="post-<?php the_ID(); ?>">
    <h3><a href="<?php the_permalink()?
   >" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title();?
   ></a></h3>
 *  <div class="entry">
    <?php the_content('Read the rest of this entry &raquo;');?
   > </div>
 *  </div>
 *  <?php endwhile; ?>
 *  <?php else : ?>
 *  <h3 class="center">Not Found</h3>
    <p class="center">Sorry, but you are looking
   for something that isn't here.</p>
 *  <?php endif; ?>
 *  [Stu Carpenter](https://wordpress.org/support/users/greatbig47/)
 * (@greatbig47)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/multiple-loops-1/#post-407637)
 * Where would you stick in the name of the category, if you wanted it to be dynamic?
   I.E…(In your case) If News and Events was a category, but it didn’t have any 
   posts (so it wouldn’t appear)?
 *  Thread Starter [Ryan Fitzer](https://wordpress.org/support/users/ryanfitzer/)
 * (@ryanfitzer)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/multiple-loops-1/#post-407667)
 * Not sure how that would work but I think I read something on it. I’ll look around
   and post back when I get it.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Multiple Loops’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [multiple](https://wordpress.org/support/topic-tag/multiple/)

 * 4 replies
 * 2 participants
 * Last reply from: [Ryan Fitzer](https://wordpress.org/support/users/ryanfitzer/)
 * Last activity: [19 years, 11 months ago](https://wordpress.org/support/topic/multiple-loops-1/#post-407667)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
