Title: Nesting a while loop in an if else statement
Last modified: August 24, 2016

---

# Nesting a while loop in an if else statement

 *  Resolved [theplastickid](https://wordpress.org/support/users/theplastickid/)
 * (@theplastickid)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/nesting-a-while-loop-in-an-if-else-statement/)
 * I have created an if-else statement to determine what posts to call based on 
   page ID.
 * If the page has an ID of 9, I want to run a while loop that will loop through
   all posts with the category ID of 3 and show 10 of these posts.
 * If the page ID is anything other than page ID 9, I want the while loop to display
   all posts from all categories except those which have an ID of 3.
 * This is what I have come up with so far, but it seems to not be outputting anything.
   I can’t quite see where I am going wrong here:
 *     ```
       <?php if ( is_page(9) ) {
       $custom_query = new WP_Query('cat=3&posts_per_page=10'); // get posts only from category 3 and display 10
       while($custom_query->have_posts()) : $custom_query->the_post();
       } else {
       $custom_query = new WP_Query('cat=-3'); // exclude category 3
       while($custom_query->have_posts()) : $custom_query->the_post(); } ?>
   
       // Content to go here
   
       <?php endwhile; ?>
       <?php wp_reset_postdata(); // reset the query ?>
       ```
   

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

 *  [istein](https://wordpress.org/support/users/istein-1/)
 * (@istein-1)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/nesting-a-while-loop-in-an-if-else-statement/#post-6091862)
 * Where are you putting this code?
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/nesting-a-while-loop-in-an-if-else-statement/#post-6091863)
 * rewrite the code to:
 *     ```
       <?php if ( is_page(9) ) {
       $custom_query = new WP_Query('cat=3&posts_per_page=10'); // get posts only from category 3 and display 10
       } else {
       $custom_query = new WP_Query('cat=-3'); // exclude category 3
       }
       while($custom_query->have_posts()) : $custom_query->the_post(); ?>
   
       // Content to go here
   
       <?php endwhile; ?>
       <?php wp_reset_postdata(); // reset the query ?>
       ```
   
 *  Thread Starter [theplastickid](https://wordpress.org/support/users/theplastickid/)
 * (@theplastickid)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/nesting-a-while-loop-in-an-if-else-statement/#post-6091881)
 * Worked perfectly alchymyth. Thank you very much for the feedback!

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

The topic ‘Nesting a while loop in an if else statement’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 3 participants
 * Last reply from: [theplastickid](https://wordpress.org/support/users/theplastickid/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/nesting-a-while-loop-in-an-if-else-statement/#post-6091881)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
