从目录中获取文件我建立一个wordpress主题,我试图从一个目录中使用php随机顺序获取所有的jpg文件.... 即时使用在win7(localhost)上的Xampp。从随机顺序使用php(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'前面的'test? – 2013-05-02 08:55:28
是的,我编辑了这个问题,谢谢!我在我的原始代码上拥有它! – Firezilla12 2013-05-02 08:57:17
尝试用'get_template_directory'替换'get_template_directory_uri',因为'get_template_directory_uri'提供了一个URI(并且你不想这么做) – EaterOfCode 2013-05-02 08:59:54