I switched my site to WordPress once I found a pleasing theme. As it happens it is the standard theme of version three – the Twenty Ten. Of course it needed some personalizing. I made a child theme in order to allow updates to the original.
The latest tweak is having a rotating featured image in the header. The original theme comes with a few images and you can pick one as the default or upload your own. You can also pick any other image to be the featured image on a page or a post. The idea here is to have a random image to appear on those pages or posts with no specified featured image. (You can see a sample on this post by hitting refresh on your browser.)
The technique used does not utilize the crop and resize capabilities available when selecting a featured image. Therefore the images used must be preprocessed to the resolution of 940×198 pixels. At least you get to make the composition yourself. The images are then uploaded to a subdirectory below the child theme.
E.g: theme/images/headers/
The header.php file of the child theme needs a small change on the row beginning with:
<img src="<?php header_image(); ?>" alt="" />
(In the original theme the row is 77.)
You change the name of the function called as follows:
<img src="<?php random_header_image(); ?>" alt="" />
Add a new function to the functions.php file:
function random_header_image(){ $sdir = '/images/headers/'; $idir = STYLESHEETPATH . $sdir; $iurl = get_bloginfo('stylesheet_directory') . $sdir; if(file_exists($idir)){ $list = scandir($idir); // count from 2 onwards to avoid . and .. if(count($list)>2) $image = $list[rand(2,count($list)-1)]; } if($image==''){ header_image(); // fallback to default function on no image returned } else { echo $iurl . $image; } }
The function calls for the location of the style file since it is the only way to locate the child theme. The names of the images in the subdirectory are gathered to an array and one is picked by random. This way there can be any number of images with any names. (However you should avoid spaces and special characters.)
If no image is found then the function calls the original function presenting the default image. Should the page or post have a specific featured image then that is identified in the header.php file prior to calling this function. Therefore this does not interfere with your specifically selected images.
NOTE! Using any cache plugin may present the user with an unchanged image.
Edited Feb 23rd, 2011: Changed rand function counter to start from two to get actual files only.
Appreciate the code snippet. I found that, for a folder containing only 2 images, in order to get proper results, the conditional should be slightly changed to:
count($list)>=2
Thanks-good work. –Bill
That’s funny. The count of two is there just to skip over the dot (.) and double-dot (..) entries in the directory content list. So basically having even one image should work since it would be the third entry.
What web server do you have and on what operating system?
Do you know if taking a ‘scandir’ of a directory contains the dots in your environment?
What I meant was that >2 only yielded the first of the two images. In order to get a choice between img1 and img2, I found that >=2 worked. This also works fine for 3 images in the header folder.
I have only run this on a localhost xampp setup, but I will get back to you when I put a test version online. I will also pay attention to what a scandir does (ie, whether it includes the dots or not).