Empty Check Foreach Loop to Display Custom Message

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


Leave a Reply

Posted in php
katherine as a flat graphic icon

About Me

I’m an African / Ojibwe First Nations Web Developer living in Winnipeg, Manitoba.

Visit the Tips and Blog to see what I’m working on.