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

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

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/

主站蜘蛛池模板: 安岳县| 柘城县| 鲁山县| 麻江县| 任丘市| 望都县| 锡林郭勒盟| 堆龙德庆县| 平定县| 深州市| 阳高县| 朔州市| 张家口市| 石阡县| 长顺县| 津市市| 涡阳县| 镇康县| 麻栗坡县| 闵行区| 舒城县| 砚山县| 同江市| 攀枝花市| 苍山县| 陇南市| 巢湖市| 红河县| 乌鲁木齐市| 天等县| 司法| 彩票| 贵南县| 会东县| 雅江县| 阳谷县| 莱芜市| 大兴区| 华阴市| 沾益县| 德阳市|