Random Image by eckert


Do you want to have a group of random images pop-up on the page using PHP?

Before we go any further, this does not allow each image to link to a different location.

Copy this code into your header:

<style type="text/css">
#randomimage { background:url(<?php bloginfo('template_directory'); ?>/images/randomimage<?php echo (rand()%10); ?>.jpg); }
</style>

Copy this code into your page where you want the images to show up:

<div id="randomimage"></div>

Copy this into your CSS and modify accordingly:

#randomimage {
}

Now, you can change the name of the div and you can also put the images wherever you want, just make sure you change the path so that they match.

One very important thing about this code is that you need to have 10 images.  Also, the images need to be named very specifically.

Using this code, the images have to be named: randomimage0.jpg, randomimage1.jpg… and continuing up through randomimage9.jpg.  If you do not have an image representing this, then nothing will show up on your page.

While not necessarily the best php image randomizer I’ve seen, it’s quick and will get the job done.

Tags: , , , , ,

Leave a Reply


It’s Called Web Design is proudly powered by WordPress
Entries (RSS) and Comments (RSS). Valid XHTML