Categories
Code Web

Improve your WordPress: related posts for 404’s

Second in a series of articles about tinkering with improving your WordPress installation, we return to custom 404 error pages; adding a list of possibly related posts when visitors have followed an outdated link. Other 404 error page improvements can be found in the first article of this series.

One of the most useful things on a 404 page is a direct link to the page visitors were trying to get to. Now we can’t read minds, but we do know the URI (explained in the third paragraph of the previous article) and that’s good enough. The following code is adapted from this archGFX article. The method used to transform the URI into a search query is very simple. If you would like a more advanced please refer to “A better 404 – Redux” at Urban Mainframe, where Jonathan Hollin expounds on his (downloadable!) 404 page code.

There are two parts to this “related posts” code. The first part makes it possible to get from “/wrong/link.html” (the URI) to “wrong link” (the search query).

Here it is:

<?php

$search_term = substr($_SERVER['REQUEST_URI'],1);

$search_term = urldecode(stripslashes($search_term));

$find = array ("'.html'", "'[-/_]'") ;

$replace = " " ;

$search_term = trim(preg_replace ( $find , $replace , $search_term ));

$search_term_q = preg_replace('/ /', '%20', $search_term);

?>

That’s all there is to it. If you, however, don’t want to make “/wrong/link.html” into “wrong link”, but rather just “link” (because the first part of the URI is likely to be useless), use the following on the fourth line:

$find = array ("'.html'", "'.+/'", "'[-/_]'") ;

The second part consists of telling WordPress to use the search terms and outputting a list of articles based on the results.

In this case the loop will output a linked title, a line-break and then the date of the article. You can edit out the line-break and the date if you want. Also, you can change OL to UL if you want an unordered list outputted.

<?php

query_posts('s='. $search_term_q );

if ( have_posts() ) :

?>

<h1>Possibly related content</h1>

<ol>

<?php while ( have_posts() ) : the_post(); ?>

<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a>

<br>

<?php unset($previousday); printf(__('%1$s', 'sandbox'), the_date('', '', '', false), get_the_time()) ?>

</li>

<?php  endwhile; ?>

</ol>

<?php else : endif; ?>

That’s all there is to it! Once you’ve added this code your 404 error page should look a little something like this.

To conclude, add the following to your 404.php, wherever you think it fits best, to add :

<?php /* Transforming the URI into search terms */

$search_term = substr($_SERVER[‘REQUEST_URI’],1);

$search_term = urldecode(stripslashes($search_term));

$find = array (“‘.html'”, “‘[-/_]'”) ;

/* If you only want the last term of the URI, use the following instead:

$find = array (“‘.html'”, “‘.+/'”, “‘[-/_]'”) ;  */

$replace = ” ” ;

$search_term = trim(preg_replace ( $find , $replace , $search_term ));

$search_term_q = preg_replace(‘/ /’, ‘%20’, $search_term);

?>

<?php /* Use the search terms to run a query */

query_posts(‘s=’. $search_term_q );

/* check to see if there are posts */

if ( have_posts() ) :

?>

<h1>Possibly related content</h1>

<ol>

<?php /* start the loop */  while ( have_posts() ) : the_post(); ?>

<li><a href=”<?php the_permalink() ?>”><?php the_title() ?></a>

<br>

<?php unset($previousday); printf(__(‘%1$s’, ‘sandbox’), the_date(”, ”, ”, false), get_the_time()) ?>

</li>

<?php /* end the loop */  endwhile; ?>

</ol>

<?php else : endif; ?>

If you’ve any questions, don’t hesitate to ask!

3 replies on “Improve your WordPress: related posts for 404’s”

That code adds the time of publication underneath (or behind, depending on how you style it) the post title. That gives people a sense of how old it is, helping them to see if it’s relevant.

Doesn’t it work for you?

Comments are closed.