<

Super Simple Random Image

This super simple piece of PHP code will randomly pull images from a folder that are numbered (ie: 1 through 9).  Technically it’s just any folder with numbered image names.   For TCS I created a folder called gallery in my theme folder and placed images in it 1.jpg, 2.jpg and so forth.

<?php
// Change this to the total number of images in the folder
$total = "9";
// Change to the type of files to use eg. .jpg or .gif
$file_type = ".jpg";
// Change to the location of the folder containing the images
$image_folder = "gallery";
// You do not need to edit below this line
$start = "1";
$random = mt_rand($start, $total);
$image_name = $random . $file_type; ?>

Next this echo will call forth the two locations.

<?php echo "$image_folder/$image_name"; ?>

I actually integrated this into the background CSS to produce a cool random background image effect for the header.

<div id="header" style="background: url('<?php bloginfo('template_directory'); ?>/<?php echo "$image_folder/$image_name"; ?>') center top no-repeat">