- WordPress 2.7 Cookbook
- Jean-Baptiste Jung
- 422字
- 2021-04-01 13:55:51
Creating a custom 404 error page
Who hasn't seen a 404 error page in his life? I'm sure there rarely is anyone. And you'll probably agree with me that 404 errors are boring—especially when you're looking for something that appears to have been moved.
This is why it is very important to have a custom and useful 404 page. In this recipe, I'll show you how to do it for your WordPress blog.

Getting ready
To achieve this recipe, you need a 404.php
file. Most WordPress themes actually feature this kind of page. If, for some reason, your theme doesn't feature such a page, simply create a php file named 404.php
and upload it to your wp-content/themes/yourtheme
directory. You don't have to add a Page template directive—WordPress automatically recognizes a file named 404.php
as a page designed to be displayed if a 404 error occurs.
In this example, we are using the 404.php
file from the default WordPress theme. If you open the file, you'll find the following code:
<?php get_header(); ?> <div id="content" class="narrowcolumn"> <h2 class="center">Error 404 - Not Found</h2> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
As you can see, this page does nothing except inform the user that nothing was found at the particular URL. Let's enhance it to help the visitor looking for some content, by displaying archives, categories, and a link to the blog homepage.
How to do it...
- Open the
404.php
file in your favorite editor. - Below the
<h2 class="center">Error 404 - Not Found</h2>
line, add the following code to create a link to the blog homepage:<a href="<?php bloginfo("url");?>">Homepage</a>
- Add the following code to display your categories:
<h2>Categories</h2> <ul> <?php wp_list_categories("title_li="); ?> </ul>
- Add the following code to display your monthly archives:
<?php wp_get_archives('type=monthly&limit=12'); ?>
- Save the file, and you're done!
How it works...
WordPress features an interesting template hierarchy mechanism. When a 404 error appears, WordPress automatically looks for a file named 404.php
. If such a file exists, it is displayed. Otherwise, the 404.php
file is displayed page .
In the 404.php
file we have displayed our blog's monthly archives and categories to make sure that the reader will find the content that he's looking for.
There's more...
In this recipe, we have seen how to create a useful 404 page. However, did you know that some bloggers also enjoy creating funny 404 pages? For example, the 404 error page of my blog Cats Who Code, features a picture of a cat trying to repair a PC with the caption, "There's a problem, but someone is fixing it".
For a list of funny an unusual 404 pages, you should take a look at the following link: http://blogof.francescomugnai.com/2008/08/the-100-most-funny-and-unusual-404-error-pages/
- iOS應用逆向與安全之道
- 架構之美
- Oracle SQL Developer 2.1
- 中文版Premiere Pro CC入門教程
- 視覺封王:Photoshop CC 2019立體化教程(素材+視頻+教案)
- Apache Roller 4.0 – Beginner's Guide
- Photoshop CS6中文版基礎培訓教程
- 中文版AutoCAD基礎培訓教程
- SolidWorks快速入門教程(2022中文版)
- Adobe創意大學Photoshop CS5 產品專家認證標準教材
- 中文版Photoshop CC基礎培訓教程
- Scribus 1.3.5: Beginner's Guide
- Altium Designer 21實戰從入門到精通
- Microsoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications
- 傳奇:ZBrush數字雕刻大師之路(第2版)