Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Do you have a suggestion on how I can get a random 404 page displayed when a page isn't found.

I use Wordpress and am currently using a custom 404.php file for displaying a 404 page but what I want is to have three or four different pages that are displayed randomly when a visitor 404s.

share|improve this question

1 Answer 1

up vote 3 down vote accepted

You can create a dynamic output using PHP inside the 404.php template.

Effectively you use the 404.php to include your header, footer and any navigation as per a normal template, and then swap out the normal loop and create a block of content that is randomised - e.g.:

<? 
$num = Rand (1,4);
switch ($num) { // Here's 4 different examples
 case 1:
 echo "<div>content goes here</div>"; //normal HTML
 break;
 case 2:
 echo $string_output_youve_set_earlier; //HTML in a string
 break;
 case 3:
 include 'random_block_3.php'; //a PHP block
 break;
 case 4:
 get_template_part('random','four'); //Using WordPress's own engine
}
?> 
share|improve this answer
    
OK, thanks for your help. Can I just use include random_block_1.php; for each of the cases? Would the name of the file then be random_block_1.php, random_block_2.php etc.? Also, if I use WP engine, how does it specify template file to be used? –  user11320 Dec 19 '11 at 12:55
    
Works flawlessly! Thanks! –  user11320 Dec 19 '11 at 21:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.