- 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/
- WordPress 2.7 Cookbook
- MATLAB計算機視覺經典應用
- 數碼攝影后期零基礎入門教程
- Adobe After Effects CC 高手之路
- 像攝影師一樣調色
- CorelDRAW X5實用教程(第2版)
- 音樂日記:Logic Pro X場景x風格編曲實用教程
- 中文版Illustrator CC 2018基礎培訓教程
- 中文版Photoshop CS6全能一本通·全彩版
- SPSS 28.0統計分析從入門到精通(升級版)
- Apache Maven 3 Cookbook
- CMS Design Using PHP and jQuery
- 邊做邊學:平面設計(Photoshop CS6 +CorelDRAW X6)
- UG NX 12.0中文版從入門到精通
- Photoshop CC圖像處理案例教程(第2版)