• 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)
Viewing 3 replies - 1 through 3 (of 3 total)

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