官术网_书友最值得收藏!

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.

Creating a custom 404 error page

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...

  1. Open the 404.php file in your favorite editor.
  2. 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>
  3. Add the following code to display your categories:
    <h2>Categories</h2>
    <ul>
    <?php wp_list_categories("title_li="); ?>
    </ul>
  4. Add the following code to display your monthly archives:
    <?php wp_get_archives('type=monthly&limit=12'); ?>
  5. 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/

主站蜘蛛池模板: 固原市| 文昌市| 监利县| 南充市| 澄江县| 增城市| 嘉禾县| 泰州市| 巧家县| 衢州市| 平昌县| 凤翔县| 抚顺县| 新建县| 利辛县| 抚远县| 信丰县| 云霄县| 贵溪市| 和林格尔县| 萨迦县| 大石桥市| 安义县| 岳西县| 河西区| 丹东市| 南江县| 天峻县| 萝北县| 台南市| 岐山县| 仲巴县| 南涧| 莆田市| 和平县| 岑溪市| 武鸣县| 延长县| 贵阳市| 沾益县| 永新县|