0

我有一些代码:

<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>\n";
    }    
?>
</ul>

问题是没有显示图像(尽管它们确实存在)。我可以绝对引用它们,但 PHP 没有找到任何东西/数组是空的。我使用 WAMP 开发网站,我开始怀疑这是否是我生活的祸根......

4

1 回答 1

0

根据评论,从该get_stylesheet_directory_uri()方法返回的路径是http://127.0.0.1/xxxx/wp-content/themes/responsive-child/.

然后直接在 PHPglob()函数中使用此路径。

简短的回答直接来自文档:

注意:此功能不适用于远程文件,因为要检查的文件必须可以通过服务器的文件系统访问。

由于您知道当前域是什么,因此可能的解决方案是从返回的路径中删除域名get_stylesheet_directory_uri()并在完整路径中使用结果:

$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');

这将带回一系列图像,您可以像当前所做的那样对其进行迭代。但是,此列表将与正在执行脚本的当前目录相关,因为它们不会以 a/或域名为前缀。因此,我们可以将其添加回foreach循环中:

foreach($images as $image) {
    $image = $domain . $image;
    // ...
}
于 2013-06-19T16:43:57.470 回答