Title: Nested loop help
Last modified: August 19, 2016

---

# Nested loop help

 *  [Marty](https://wordpress.org/support/users/mrthrust/)
 * (@mrthrust)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/nested-loop-help/)
 * Hi,
 * I’ve been trying to implement a small nested loop on my wordpress homepage, local
   copy only just now(no url)…
 * What im trying to achieve is this:
 * check what month it is; ie: current month = 1 //Jan
    check what posts have this
   as a category… show 5 posts IF there is any, If there’s none, then move onto 
   the next month(category) and show 5 from that…
 * code is below..
    **functions.php**
 *     ```
       function get_current_month_category()
       {
       	$thismonth = date("M");
       	//$thismonth = "Feb";
       	//$thecat = $thismonth;
       	//echo $thismonth;
       	switch($thismonth) {
       		case "Jan" : $thecat = "23";
       		case "Feb" : $thecat = "7";
       		case "Mar" : $thecat = "8";
       		case "Apr" : $thecat = "9";
       		case "May" : $thecat = "27";
       		case "Jun" : $thecat = "31";
       		case "Jul" : $thecat = "24";
       		case "Aug" : $thecat = "20";
       		case "Sept" : $thecat = "15";
       		case "Oct" : $thecat = "16";
       		case "Nov" : $thecat = "17";
       		case "Dec" : $thecat = "21";
       		default : $thecat = "23"; // Jan
       	}
       	return $thecat;
       }
       ```
   
 * // Code on the Homepage
 *     ```
       <div>
       <?php
       $thismonth = get_current_month_category();
       echo "<!-- Current Month Category: " . $thismonth."-->";
       $sql = "cat=".$thismonth."&showposts=5&order=ASC";
       $my_query = $wpdb->get_results($sql, OBJECT);
   
       if ($my_query): foreach ($my_query as $post): setup_postdata($post);
       $do_not_duplicate = $post->ID;
       ?>
       <ul style="position: relative;">
       <li>
       <h4><a class="continue" href="<?php the_permalink(); ?>">
       <?php the_title();?></a></h4>
       </li>
       <li>
          <strong>Date</strong>:
          <?php echo c2c_get_custom('Event-Day'); ?>/
          <?php echo c2c_get_custom('Event-Month'); ?>  @
          <?php echo c2c_get_custom('Event-Time'); ?>
       </li>
       </ul>
       <?php wp_reset_query(); ?>
       <?php endforeach; //endwhile; ?>
       <?php else : ?>
       <h4>Nothing this Month - Viewing Next Month</h4>
       <ul style="position: relative;">
       <?php
       $nextmonth = get_next_month_category();
       $args=array(
       	'cat'=>$nextmonth,
       	'showposts' => '10',
       	'order' => 'ASC'
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post();
       ?>
       <li><h4><a class="continue" href="<?php the_permalink(); ?>"><?php the_title();?></a></h4></li>
       <li style="position: absolute; top: 0px; right: 5px;">
       <strong>Date</strong>:
       <?php echo c2c_get_custom('Event-Day'); ?>/
       <?php echo c2c_get_custom('Event-Month'); ?>  @
       <?php echo c2c_get_custom('Event-Time'); ?>
       </li>
       <?php endwhile; } ?>
       </ul>
       <?php endif; ?>
       </div>
       ```
   
 * so basically on the first part of the code, it checks for any posts which belongs
   to the category ie: Jan, if there is then show 5 posts from that category, if
   there is none, then check for any posts from the next month(category), if there
   is is then loop through 5 of them, using a new query?
 * should i be using the setup_postdata and a new query, or should the query be 
   continued from the first check?
 * thanks for any help

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

 *  Thread Starter [Marty](https://wordpress.org/support/users/mrthrust/)
 * (@mrthrust)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/nested-loop-help/#post-1348699)
 * what i need is
 * ‘check for posts from category’
 * if posts
    -  loop
       end loop
 * else ( // much the same as – Sorry no posts found )
 * query from other category
    if posts
    -  loop
       end loop
 * endif
 * end first if
 *  Thread Starter [Marty](https://wordpress.org/support/users/mrthrust/)
 * (@mrthrust)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/nested-loop-help/#post-1348867)
 * anyone.?
 *  [axelator010101](https://wordpress.org/support/users/axelator010101/)
 * (@axelator010101)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/nested-loop-help/#post-1348959)
 * No help yet? Darn, I could learn something from this too.

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

The topic ‘Nested loop help’ is closed to new replies.

## Tags

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

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

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
