Step 1 Introduction
Ever imagined how one can tweak WordPress codes and get awesome results? Today I will teach you one such tweak on how to add random images to header in WordPress without any plugin. But before that, let us know something about header images. Usually those images are static. You will probably see numerous WordPress blogs that has dynamic header images which rotate randomly.
Step 2 Now let us start the tweak.
First select the images which you want to put in header section. Make sure all your images have same extension. If you have one image in .jpg format then all other images should be in .jpg format. Here we will be taking only 3 random images (as shown below). You can rotate any number of images.
Step 3 Selecting Images
Here, when you select your images to put in header then you should name them in a sequence like:
Following above naming structure will be beneficial while traversing images. You should separate the names with an underscore. You can change the header image text to himage or anything you like.
After that you need to paste following code into header.php where you would like the header image to be displayed.
<img src="http://path_to_image/headerimage_<?php echo(rand(1,3));?>.jpg" width="image_width" height="image_height" alt="image_alt_text"/>
Step 4 Header.php
Here in above code rand(1,3) will generate any random number between 1 and 3(including both 1 and 3). You can now see why naming structure had been important. You just need to change the sequence number of images which will be the filename of images. That becomes a lot easier, isn’t it?
Now if you change the number of images in header, make sure you change numbers inside random function. It works on php based platform.