I include this logic in my template all the time
if ( have_posts() ):
//show content
else:
//show content not found
endif;
But recently I began to doubt its necessity, WordPress will turn to 404.php when no post found, it seems no need to add this logic in normal templates, else
will never be triggered, is it?
When I look at
WP::handle_404()
, I think that the404.php
template will not be loaded, even if there are no posts, if:So in these template files,
have_posts()
can be useful.No, in most cases it’s not needed.
if ( have_posts() )
would only be useful on the index.php file, if you don’t have a 404.php template.And even in that case, you could use is_404() instead.