Aight, first post on a new Wordpress install. It seems like every time I pick a new theme I need to make some modification to the code. I guess it’s my way of leaving my mark. This time around, I chose to modify the image rotator. From the Neoclassical Theme. For those of you who dont know, the current code looks like this:
<?php
$random_image = rand(1,5); // the second number should equal the total number of images that you want to rotate
?>
<img src=”<?php bloginfo(’template_url’); ?>/headers/header_<?php echo $random_image; ?>.jpg” alt=”Random header image… Refresh for more!” />
it’s simple, and it gets the job done, but it means that whenever you want to add a new image, change and old image, or delete an image you need to rename it by a given pattern, and modify a script. My script fixes all that by scanning the directory and building an array that is filtered to make a list of images that can be used. my code looks like this:
<?php
$excluded_files = array(”exclude_me.jpg”); //Put the name of any file that you DON’T want to see in here, this includes non image files, but not dirs
$dir = TEMPLATEPATH.’/headers’;
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
$dir_contents[] = $filename;
}
foreach ($dir_contents as $file) { //This loop filters the array
if (!is_dir(TEMPLATEPATH.’/headers/’.$file)){
if (!in_array($file,$excluded_files)) {
if (str_replace(’.jpg’,’.notjpg’,$file) != $file || str_replace(’.jpeg’,’.notjpeg’,$file) != $file) { //does both .jpg and .jpeg
if ($file != ‘.’ && $file != ‘..’ && $file != NULL){
$images[] = $file;
}
}
}
}
}
$random_image = $images[rand(0,count($images)-1)]; //no more changing this
?>
<img src=”<?php bloginfo(’template_url’); ?>/headers/<?php echo $random_image; ?>” alt=”Random header image… Refresh for more!” />
It’s much longer, but it is less demanding on the end user, and will save time an energy when you decide to change the default images.
at a glance:
- php 4 compatable
- works with default installs of neoclassical, changed images, or basically anything else
- easy to install
installation:
- Download
- unpack
- upload and replace the original “rotating_images.php” (found in: “<WORPRESS DIR>/wp-content/themes/neoclassical/”) file with the new one