December 07, 2016
Sometimes when building arrays for directories, or archives, we have to list all of the content (even the empty ones).
In the unfortunate circumstance when you have no choice but to use the foreach loop, here is a handy workaround to check if the loop is empty, and display a custom message:
$args = array( 'numberposts' => -1, 'category_name' => 'blog', 'orderby' => 'author', 'order' => 'ASC' ); $temp_loop = get_posts($args); // print_r(count($temp_loop)); if(count($temp_loop) == 0) { ?> <p>Place your custom text here!</p> <?php } foreach ($temp_loop as $post) : setup_postdata($post);