【发布时间】:2013-04-26 09:07:42
【问题描述】:
我正在构建一个 wordpress 主题,我正在尝试使用 php 以随机顺序从目录中获取所有 jpg 文件...。 我在 win7 (localhost) 上使用 Xampp。
这是代码:
<?
$dir = get_template_directory_uri().'/images/top/';
$file_display = array ('jpg', 'jpeg');
if(file_exists($dir) == false){
echo 'Directory \'', $dir, '\' not found';
} else {
$dir_contents = scandir($dir);
shuffle($dir_contents);
foreach ($dir_contents as $file) {
$file_type = strtolower(end(explode('.', $file)));
if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true){
echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />';
}
}
}
?>
我总是得到
Directory 'http://localhost/ni/wp-content/themes/A/images/top/' not found
我也尝试过改变
$dir = get_template_directory_uri().'/images/top/';
到:
$dir = "C:\xampp\htdocs\Ni\wp-content\themes\A\images\top\";
但仍然没有运气,任何帮助将不胜感激!
【问题讨论】:
-
(file_exists($dir) == false)测试前面有没有和if? -
是的,我编辑了这个问题,谢谢!我在我的原始代码上有它!
-
尝试将
get_template_directory_uri替换为get_template_directory,因为get_template_directory_uri提供了一个URI(而您不希望这样) -
@EaterOfCorpses 自 PHP 5.0 起
file_exists接受 HTTP(在其他一些协议中),因此只要 OP 使用 PHP5 就没有关系。 -
使用 get_template_directory() 输出:C:\xampp\htdocs\Ni/wp-content/themes/A/images/top/ 带有混合斜杠(linux windows syle?),我得到目录''没找到
标签: php wordpress xampp wordpress-theming