【问题标题】:WordPress failed to open directory error to read folderWordPress 无法打开目录错误以读取文件夹
【发布时间】:2013-09-02 12:22:48
【问题描述】:

您好,我已经编写了用于从 WordPress 中的文件夹中获取图像的代码,但路径似乎不起作用。这是我的代码。在我的主题文件夹中有图像,然后是 empl 文件夹,但它给出错误消息“无法打开目录:没有这样的文件或目录”,我什至尝试了 php 的服务器文档根目录,但它不起作用。我的文件位于 page-template 目录中。

$string =array();
$filePath=bloginfo('template_url').'/images/empl';
echo $filePath;  
$dir = opendir($filePath);
echo $dir;
while ($file = readdir($dir)) { 
   if (preg_match("/.png/",$file) || preg_match("/.jpg/",$file) || preg_match("/.gif/",$file) ) { 
   $string[] = $file;
   }
}
$i=0;
while (sizeof($string) != 0 ){
    echo $i;
  $img = array_pop($string);
  echo "<div class='employee'><img src='$filePath$img'  data-src='$filePath$img'></div>";

  if($i>24)break;
  $i++;
}

【问题讨论】:

    标签: wordpress opendir


    【解决方案1】:

    我使用

    找到了解决方案
    $filePath=get_template_directory().'/images/empl';
    

    【讨论】:

      【解决方案2】:

      你必须使用get_bloginfo() 而不是bloginfo(),像这样:

      $filePath=get_bloginfo('template_url').'/images/empl';
      

      为什么?来自the codex

      [bloginfo()] 总是将结果打印到浏览器。如果您需要以下值 在 PHP 中使用,使用 get_bloginfo()。

      【讨论】:

        猜你喜欢
        • 2019-08-28
        • 2015-10-08
        • 2018-07-18
        • 2018-11-12
        • 1970-01-01
        • 2011-11-19
        • 1970-01-01
        • 2022-10-13
        相关资源
        最近更新 更多