Title: multiple loops &gt; php help
Last modified: August 19, 2016

---

# multiple loops > php help

 *  [goatgash](https://wordpress.org/support/users/goatgash/)
 * (@goatgash)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-gt-php-help/)
 * Hi
 * I need 3 separate loops on my category pages – one that shows the latest 5 posts
   from the category, another that shows the 5 before that, and then another that
   shows the 12 before that. I have 42 categories.
 * Currently, for each section, I am running through all 42 categories in one huge
   if statement. That means 3 huge if statements for each category page. I find 
   it hard to believe that I am doing it in the most efficient way! Any help would
   be greatly appreciated 🙂
 * Example of the current code:
 *     ```
       <?php if (is_category('1')) : ?>
   
       <?php $recent = new WP_Query("cat=1&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
   
       "><?php the_title(); ?>
       <?php endwhile; ?>
   
       <?php elseif (is_category('2')) : ?>
   
       <?php $recent = new WP_Query("cat=2&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
   
       "><?php the_title(); ?>
       <?php endwhile; ?>
   
       ...
   
       <?php elseif (is_category('42')) : ?>
   
       <?php $recent = new WP_Query("cat=42&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
   
       "><?php the_title(); ?>
       <?php endwhile; ?>
   
       <?php endif; ?>
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [goatgash](https://wordpress.org/support/users/goatgash/)
 * (@goatgash)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-gt-php-help/#post-873541)
 * Mangle the code above – should read:
 *     ```
       <?php if (is_category('1')) : ?>
   
       <?php $recent = new WP_Query("cat=1&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
   
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       <?php endwhile; ?>
   
       <?php elseif (is_category('2')) : ?>
   
       <?php $recent = new WP_Query("cat=2&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
   
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       <?php endwhile; ?>
   
       ...
   
       <?php elseif (is_category('42')) : ?>
   
       <?php $recent = new WP_Query("cat=42&showposts=12&offset=10"); while($recent->have_posts()) : $recent->the_post(); ?>
   
       <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
       <?php endwhile; ?>
   
       <?php endif; ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘multiple loops > php help’ is closed to new replies.

## Tags

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

 * 1 reply
 * 1 participant
 * Last reply from: [goatgash](https://wordpress.org/support/users/goatgash/)
 * Last activity: [17 years, 8 months ago](https://wordpress.org/support/topic/multiple-loops-gt-php-help/#post-873541)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
