{"id":1348526,"date":"2010-01-18T11:13:41","date_gmt":"2010-01-18T11:13:41","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/nested-loop-help\/"},"modified":"2016-08-19T15:19:57","modified_gmt":"2016-08-19T15:19:57","slug":"nested-loop-help","status":"closed","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/nested-loop-help\/","title":{"rendered":"Nested loop help"},"content":{"rendered":"<p>Hi, <\/p>\n<p>I&#8217;ve been trying to implement a small nested loop on my wordpress homepage, local copy only just now(no url)&#8230;<\/p>\n<p>What im trying to achieve is this:<\/p>\n<p>check what month it is; ie: current month = 1 \/\/Jan<br \/>\ncheck what posts have this as a category&#8230;<br \/>\nshow 5 posts IF there is any, If there&#8217;s none,<br \/>\nthen move onto the next month(category) and show 5 from that&#8230;<\/p>\n<p>code is below..<br \/>\n<strong>functions.php<\/strong><\/p>\n<pre><code>function get_current_month_category()\n{\n\t$thismonth = date(&quot;M&quot;);\n\t\/\/$thismonth = &quot;Feb&quot;;\n\t\/\/$thecat = $thismonth;\n\t\/\/echo $thismonth;\n\tswitch($thismonth) {\n\t\tcase &quot;Jan&quot; : $thecat = &quot;23&quot;;\n\t\tcase &quot;Feb&quot; : $thecat = &quot;7&quot;;\n\t\tcase &quot;Mar&quot; : $thecat = &quot;8&quot;;\n\t\tcase &quot;Apr&quot; : $thecat = &quot;9&quot;;\n\t\tcase &quot;May&quot; : $thecat = &quot;27&quot;;\n\t\tcase &quot;Jun&quot; : $thecat = &quot;31&quot;;\n\t\tcase &quot;Jul&quot; : $thecat = &quot;24&quot;;\n\t\tcase &quot;Aug&quot; : $thecat = &quot;20&quot;;\n\t\tcase &quot;Sept&quot; : $thecat = &quot;15&quot;;\n\t\tcase &quot;Oct&quot; : $thecat = &quot;16&quot;;\n\t\tcase &quot;Nov&quot; : $thecat = &quot;17&quot;;\n\t\tcase &quot;Dec&quot; : $thecat = &quot;21&quot;;\n\t\tdefault : $thecat = &quot;23&quot;; \/\/ Jan\n\t}\n\treturn $thecat;\n}<\/code><\/pre>\n<p>\/\/ Code on the Homepage<\/p>\n<pre><code>&lt;div&gt;\n&lt;?php\n$thismonth = get_current_month_category();\necho &quot;&lt;!-- Current Month Category: &quot; . $thismonth.&quot;--&gt;&quot;;\n$sql = &quot;cat=&quot;.$thismonth.&quot;&amp;showposts=5&amp;order=ASC&quot;;\n$my_query = $wpdb-&gt;get_results($sql, OBJECT);\n\nif ($my_query): foreach ($my_query as $post): setup_postdata($post);\n$do_not_duplicate = $post-&gt;ID;\n?&gt;\n&lt;ul style=&quot;position: relative;&quot;&gt;\n&lt;li&gt;\n&lt;h4&gt;&lt;a class=&quot;continue&quot; href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;\n&lt;?php the_title();?&gt;&lt;\/a&gt;&lt;\/h4&gt;\n&lt;\/li&gt;\n&lt;li&gt;\n   &lt;strong&gt;Date&lt;\/strong&gt;:\n   &lt;?php echo c2c_get_custom(&#039;Event-Day&#039;); ?&gt;\/\n   &lt;?php echo c2c_get_custom(&#039;Event-Month&#039;); ?&gt;  @\n   &lt;?php echo c2c_get_custom(&#039;Event-Time&#039;); ?&gt;\n&lt;\/li&gt;\n&lt;\/ul&gt;\n&lt;?php wp_reset_query(); ?&gt;\n&lt;?php endforeach; \/\/endwhile; ?&gt;\n&lt;?php else : ?&gt;\n&lt;h4&gt;Nothing this Month - Viewing Next Month&lt;\/h4&gt;\n&lt;ul style=&quot;position: relative;&quot;&gt;\n&lt;?php\n$nextmonth = get_next_month_category();\n$args=array(\n\t&#039;cat&#039;=&gt;$nextmonth,\n\t&#039;showposts&#039; =&gt; &#039;10&#039;,\n\t&#039;order&#039; =&gt; &#039;ASC&#039;\n);\n$my_query = null;\n$my_query = new WP_Query($args);\nif( $my_query-&gt;have_posts() ) { while ($my_query-&gt;have_posts()) : $my_query-&gt;the_post();\n?&gt;\n&lt;li&gt;&lt;h4&gt;&lt;a class=&quot;continue&quot; href=&quot;&lt;?php the_permalink(); ?&gt;&quot;&gt;&lt;?php the_title();?&gt;&lt;\/a&gt;&lt;\/h4&gt;&lt;\/li&gt;\n&lt;li style=&quot;position: absolute; top: 0px; right: 5px;&quot;&gt;\n&lt;strong&gt;Date&lt;\/strong&gt;:\n&lt;?php echo c2c_get_custom(&#039;Event-Day&#039;); ?&gt;\/\n&lt;?php echo c2c_get_custom(&#039;Event-Month&#039;); ?&gt;  @\n&lt;?php echo c2c_get_custom(&#039;Event-Time&#039;); ?&gt;\n&lt;\/li&gt;\n&lt;?php endwhile; } ?&gt;\n&lt;\/ul&gt;\n&lt;?php endif; ?&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<p>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?<\/p>\n<p>should i be using the setup_postdata and a new query, or should the query be continued from the first check?<\/p>\n<p>thanks for any help<\/p>\n","protected":false},"template":"","class_list":["post-1348526","topic","type-topic","status-closed","hentry","topic-tag-nested-loop"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1348526","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/1348526\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=1348526"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}