Hey guys,
I apologize in advance if this is a simple question, but I’m having trouble counting the number of images in a directory. I’m working in WordPress and I’m just looking to load a random image out of a folder directory based on what page you’re on. If the page you’re one doesn’t have a corresponding folder of images, it will just look in a default folder. Here’s what I’ve got:
[code=php]function random_background($id) {
$slugdirectory = new_slug($id); //a function that pulls the page’s slug
$dir = bloginfo(‘stylesheet_directory’);
$directory = $dir.’/images/page-backgrounds/’.$slugdirectory.’/’;
if (glob(“$directory*.jpg”) == false) {
$directory = $dir.’/images/page-backgrounds/default/’;
}
$count = 0;
$filenames = array();
$filepath = $directory . “*”;
foreach (glob($filepath) as $filename) {
array_push($filenames , $filename);
$count++;
}
$rand = rand(0, ($count-1));
return $direcotry.”.$filenames[$rand];
}
For one reason or another, my files don’t see to be getting counted and the array $filenames just get returns blank.
Does anybody have any bright ideas? Thanks in advance.