WordPress 404

Most WordPress themes, including the ones I write have pretty 404 pages, yet they don’t have a pretty “no posts found” page.

I like to show the 404 page when no articles are found instead of the boring “Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.” message

A typical theme posts page/archive would look like this;

get_header(); 
if ( have_posts() ) :
 
 while ( have_posts() ) : the_post(); 
  // do posty showy stuff
 
 endwhile;
else
 // no posts found
  _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.', 'twentyeleven' ); 
endif;
get_footer();

We have a really nice 404 page that hardly ever gets seen, lets use it more.

if ( have_posts() ) :
get_header();
 while ( have_posts() ) : the_post(); 
  // do posty showy stuff
 endwhile;
get_footer();
else
 // no posts found
 status_header(404);
 nocache_headers();
 include( get_404_template() );
 exit;
endif;

 

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.