I have some code:
<div id="image-cycle-container">
<ul id="image-cycle">
<?php
//Select the folder which the images are contained within and store as an array
$imgDir = get_stylesheet_directory_uri() . '/img/image-cycle/';
$images = glob($imgDir . '*.jpg');
foreach($images as $image){
echo 'something';
echo "<li><img src='".$image."' /></li>
";
}
?>
</ul>
and the problem is that no images are displayed (although they definitely do exist). I can reference them absolutely but PHP is not finding anything/ the array is empty. I use WAMP for developing sites and I'm starting to wonder if this is the bane of my life...
Per a comment, the path being returned from the get_stylesheet_directory_uri()
method is http://127.0.0.1/xxxx/wp-content/themes/responsive-child/
.
This path is then used directly in the PHP glob()
function.
The short answer comes directly from the documentation:
Note: This function will not work on remote files as the file to be examined must be accessible via the server's filesystem.
A possible solution to this, since you know what your current domain is, would be to strip the domain-name from the path returned from get_stylesheet_directory_uri()
and use the result in the full-path:
$domain = 'http://127.0.0.1/';
$imgDir = get_stylesheet_directory_uri() . '/img/image-cycle/';
$imgDir = substr($imgDir, strlen($domain)); // strip the domain
$images = glob($imgDir . '*.jpg');
This will bring back an array of images that you can iterate over as you're currently doing. However, this list will be relative to the current directory that the script is executing in because they won't be prefixed with a /
or the domain name. So, we can add it back in the foreach
loop:
foreach($images as $image) {
$image = $domain . $image;
// ...
}