I'm trying to load a random image from my child them images directory each time the page loads.
I'm loading it as a shortcode right now. This is the code i'm using:
function footer_image () {
$imagesDir = get_stylesheet_directory_uri().'/images/footer/';
$imagespec = get_stylesheet_directory_uri().'/images/footer/footer-fishing.png'; //for testing
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)].'there';
return $randomImage;//this returns nothing.
return $imagespec; //this returns the correct url for the image
return $imagesDir; //this returns the correct url for the images directory
}
add_shortcode ('footer-image', 'footer_image');
in the end I want to use return $randomImage
, but in my testing i've commented out that line and individually added each of the other returns to see if they are working. They both do.
So it would appear I have something wrong with the $images
or $randomImage
variable.
$images’ then pump it into your display with
print_r()` or something like that so you can see what it has stored. In the$randomImage
declarationarray_rand()
can return more than one value unless you set the second parameter to 1. Also adding the string ‘there’ onto the end of$randomImage
looks like it might be a mistake. – Ben HartLenn Jan 19 '18 at 19:37