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)
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.