Posted by on December 15, 2010

First, creat a page called 404.php, and put the file under your theme root. For example, you are using the wordpress theme called twentyten, then please put 404.php under wp-content/themes/twentyten.

Add the following code at 404.php:

<?php get_header(); ?></p>
<h1><?php bloginfo(‘url’); ?></h1>   <!– get domain url –>

<ul>
<?php
//$rand_posts = get_posts(‘numberposts=-1&orderby=title&order=ASC’);  // get all posts.
$rand_posts = get_posts(‘numberposts=5&orderby=rand’);

foreach( $rand_posts as $post ) :
?>
<li><a title=”<?php the_title(); ?>” href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endforeach; ?>

 

<?php
$rand_posts = get_posts(‘numberposts=5&orderby=rand’);
foreach( $rand_posts as $post ) :
?>
<li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>

<ul>
<?php query_posts(‘posts_per_page=999’); 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>
<p><?php get_footer(); ?>

Second, open .htaccess at your blog root,  if you do not have .htaccess, you have to creat one, and add the following line at the file:

ErrorDocument 404 /404.php

Done!

You also can modify 404.php code to meet your special demands.

Posted in: PHP Skills, WordPress