Try to see if this works, I used scandir
because I prefer it. The problem was that glob
, like scandir
, only works with paths on the server’s file system, not URLs:
function footer_image(){
$imagesDir = get_stylesheet_directory() . '/images/footer';
$imagesUri = get_stylesheet_directory_uri() . '/images/footer/';
$images = array_diff( scandir( $imagesDir, 1 ), array( '.', '..' ) );
$randomImage = $images[ array_rand( $images ) ];
$footerImage="<img src="" . $imagesUri . $randomImage . '" alt="Footer image">';
return $footerImage;
}
add_shortcode ( 'footer-image', 'footer_image' );
To use a shortcode in a page/theme template:
echo do_shortcode("[footer-image]");
P.S. Instead, if you created a taxonomy in the media library for example footer
and took those images, you could use more attributes (alt
, description
, size
, etc.), improve SEO and make the website more responsive. What do you think about it?