How To:Display a Random header image in WordPress

By | December 7, 2013

Many blog have different header image which keep on changing from page to page, which give the site a rich look. Here’s a simple random function which displays different header images on your WordPress site. To achieve this php rand () function is used, which allows us to get a random number between 1 and 99.
If the random number is less than or equal to 33, the first header image will be displayed. Less the 66, the second header image will be displayed, and then the 3 will be showed if it is less than or equal to 99.

Paste this code in your header.php file:

$result_random=rand(1, 99);

if($result_random<=33){ ?>
<a href="" title="LiveTUT" rel="home">
<img src="" alt="LIVETUT" />

<?php }

elseif($result_random<=66){ ?>
<a href="" title="LiveTUT" rel="home">
<img src="" alt=LIVETUT"/>

<?php }

elseif($result_random<=99){ ?>
<a href="" title="LiveTUT" rel="home">
<img src="" alt="LIVETUT"/>

<?php } ?>

Method two:
In this your header image can be placed in a folder with different extension numbers. Such as
Now add this code to your header.php file where your image likes to be displayed.
Paste this code:

<img src="http://path_to_images/headerimage_<?php echo(rand(1,3)); ?>.jpg"
width="image_width" height="image_height" alt="image_alt_text" />

Make sure you change the image as 1,2,3 with proper extension.

Leave a Reply

Your email address will not be published. Required fields are marked *

Human Verification: In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.