Title: Nested loops
Last modified: August 19, 2016

---

# Nested loops

 *  [ziguline](https://wordpress.org/support/users/ziguline/)
 * (@ziguline)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/nested-loops-1/)
 * Hi,
 * I’m trying a solution in order to do the following action:
 * 1 – I’ve a first query that should display a post with a specific tag
 *  If there are no post with that specific tag
 * 2 – run another query in order to display other kind of posts
 * Looking at the documentation I didn’t find any solution.. or I’m not able to 
   understand that 🙁
 * Thank you for your precious help.
 * Dimitri
 * ziguline.com

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

 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/nested-loops-1/#post-1324453)
 * I think you want something like this:
 *     ```
       <?php $counter = 0; ?>
       <?php if (have_posts()) { while (have_posts()) : the_post(); ?>
             <?php ++$counter; ?>
             <!-- do stuff for first loop -->
          <?php endwhile; ?>
       <?php } // End of first loop ?>
       <!-- Do other stuff before second loop -->
       <?php if (!$counter) : ?>
          <?php if (have_posts()) { while (have_posts()) : the_post(); ?>
                <!-- Do stuff for second loop -->
             <?php endwhile; ?>
          <?php } // End of second loop ?>
       <?php endif; // End of if $counter ?>
       ```
   
 *  Thread Starter [ziguline](https://wordpress.org/support/users/ziguline/)
 * (@ziguline)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/nested-loops-1/#post-1324455)
 * I’ll try it out and I’ll let you know 😀
 * thank you so much!
 * Dimitri
 * ziguline.it

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

The topic ‘Nested loops’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [ziguline](https://wordpress.org/support/users/ziguline/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/nested-loops-1/#post-1324455)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
