Skip to content

WordPress tips and tricks – custom 404 (error) pages

One of the more underused features in wordpress themes is the ability to create and modify the 404 page – so to try to rectify this I thought I’d run through what I do with 404 pages on Binary Moon (and my other WordPress site). I use this simple technique to try to make errors a bit more and helpful for people who have ended up at the wrong place. I’m hoping that not too many people see the 404 page but just in case here is what I’ve done.

Basically I duplicate a normal page template and name it “404.php”. I then remove “the loop” and add some some static code which explains that there has been an error and a few possible solutions (archives, search).

Finally I use query_posts to add in a new loop which displays links to the 5 most recent articles. Below is the actual code I use on this site for my 404 page (you can see it in action here – http://www.binarymoon.co.uk/9837489)

<?php get_header(); ?>
	
	<h1>404 - Doh!</h1>
	
	<p>Something has gone wrong and the page you're looking for can't be found.</p>
	<p>Hopefully one of the options below will help you</p>
	
	<ul>
		<li>You can search the site using the search box to the left</li>
		<li>You could visit <a href="http://www.binarymoon.co.uk/">the homepage</a></li>
		<li>Maybe what you're looking for is </a><a href="http://www.binarymoon.co.uk/archives/">in the Archives?</a></li>
		<li>Or you could have a look through the recent posts listed below, maybe what you're looking for is there</li>
	</ul>
	
	<h3>Recent Posts</h3>
	
	<ul>
	<?php
	query_posts('posts_per_page=5');
	if (have_posts()) : while (have_posts()) : the_post(); ?>
	<li><a href="<?php the_permalink() ?>" title="Permalink for : <?php the_title(); ?>"><?php the_title(); ?></a>
	<?php endwhile; endif; ?>
	</ul>
	
<?php get_footer(); ?>

Something you may want (need?) to do is to add your new 404 page to your .htaccess file so that when a bad/ non-existant page is accessed the visitor gets your custom 404 instead of your webhosts ugly page. To do this all you need to do is grab your .htaccess file and add the following code.

ErrorDocument 404 /index.php?error=404

That’s it – easy as can be.

Now nobody has an excuse not to have a helpful, attractive 404 page. Of course it doesn’t have to be helpful (but that IS nice) but can also be funny or amusing – maybe you can find some inspiration here at area 404. Be sure to let me know if you have an unusual or unique 404 page as well.

Share

24 Comments »

  1. Pingback: Everyone should have at least one Gotchi! » Angepasste 404 Site
  2. Good Article.

    Actually you do not need to use query posts for this purpose.
    you could very well do wp_get_archives(‘type=postbypost&limit=6′) to pull the latest 6 posts.

    Thanks

  3. Sadish – thanks for the tip. I actually wrote my own 404 page a long time ago (at least 2 years) and as far as I know the wp_get_archives command wasn’t around – and if it was around I wasn’t aware of it :)

  4. For whatever reason, the default WordPress theme created a .htaccess file on my server, but the code was very different from yours. I looked up a how-to on my server’s knowledgebase, but they suggested linking directly to the 404.php file (which didn’t work). So thanks for the index.php?error=404 information. Worked instantly.

  5. What directory do I put the 404.php in?
    I don’t see any .htaccess file anywhere.
    I have WordPress installed in the root directory, so WordPress is the main page.

  6. Hey there, thanks so much for this code suggestion. I used it two weeks ago and it worked like a charm for me, and I just suggested it to someone at #wordpress chat. Thanks!

  7. Hi Ben!
    I have found this article very interesting, and I tried to get inspired from it to load my 404 custom page, but I failed :(
    All I want to do is to tell to my WordPress blog to get a specific illustration I have made on purpose. Here’s the not-working code I have written (clearly I am not a geek) on my 404.php file (of course I have put the illustration with that name in the directory “images”), can you somehow help me to correct it, please?

    <img src="/images/error-404-700.jpg” alt=”404. Page not found.” />

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

I seem to find myself working with Youtubes thumbnail images quite a lot (for instance on Miniclips videos section) – and I am always having to go searching for the parameters to use to generate those thumbnail images. So I […]