Improve Your 404 Page Template in WordPress
When a visitor tries to access a page that does not exist, user will be redirected to 404 page. Putting some effort in understanding how to put this page into use and re-designing it to align with your brand identity, and website structure, will make your website more user friendly and will retain users from leaving your site because they ended up on a 404 page.
Some things to keep in mind for your 404 page:
- Keep your 404 error page free of advertising
- The 404 page should reflect the personality of the site
- The 404 page should include a call to action or a redirect to your website
- It be static HTML and not contain complex scripts that might create additional errors for end user
- 404 pages should load as quickly as possible
In this article I’m going to walk you through how does a default 404 page look , and how to create a 404 page in case your WordPress theme doesn’t include a 404 page to even start with. Additionally, I will be walking you through step by step tutorial with code snits that you can copy and past to your theme.
- How to Create a Custom 404 Page in WordPress
- How to Display Recent Posts on 404 Page
- How to Display Random Posts on 404 Page
-
How to Create a Custom 404 Page in WordPress
Before you start, I would recommend to backup your wordpress theme, because to create a custom 404 page template, we need to edit the 404.php file in the theme.
Now, to to take advantage of the lost click directing to your 404 page, we need to make the page more useful, I’m are going to show you how to add useful elements such as popular posts, most commented posts, recent posts, date archives, list of all categories. By enhancing your 404 you will redirect miss leaded users to the right page, or your most popular pages.
-
How to Display Recent Posts on 404 Page
There are multiple ways to display recent posts in WordPress. The easiest way is to add a template tag in your 404 template to display your recent posts, as follows:
<?php wp_get_archives( array( 'type' => 'postbypost', 'limit' => 10, 'format' => 'custom', 'before' => '', 'after' => '
' ) ); ?>
-
How to Display Random Posts on 404 Page
If you want to display a random list of posts on your 404 page, then you simply need to add this code in your 404 template where you want to display a random list of posts from your site.
<ul>
<?php
$posts
= get_posts(
'orderby=rand&numberposts=5'
);
foreach
(
$posts
as
$post
) { ?>
<li><a href=
"<?php the_permalink(); ?>"
title=
"<?php the_title(); ?>"
><?php the_title(); ?></a>
</li>
<?php } ?>
</ul>