当路径正确时没有加载图像PHP,Wordpress


Images not loading when path is correct PHP, Wordpress

我有一个加载一些图像的脚本,还有一个$dir变量,它为这些图像提供了一个绝对路径(然后查找.jpg.png文件)。

$dir = dirname(__FILE__) . '/assets/img/universities/';

文件的路径如下:

/www/html/uniswales.ac.uk/htdocs/wp/wp-content/themes/roots-master/assets/img/universities/University1.jpg

图像的路径是正确的,但不会加载图像。我认为这是一个权限问题,但不能确定。

这适用于我从头开始构建的网站,但在Wordpress网站上使用时却不行。

目录中img文件的权限为644,目录为755

更新

当我回显$dir http://www.uniswales.ac.uk/assets/img/universities/

这是我的全部脚本:

 $a = array();
                $unis = array(  "http://www.aber.ac.uk/en/", 
                                "http://www.bangor.ac.uk/",
                                "http://www.cadarn.ac.uk/",
                                "http://www3.cardiffmet.ac.uk/", 
                                "http://www.cardiff.ac.uk/", 
                                "http://www.glyndwr.ac.uk/",
                                "http://www.open.ac.uk/",
                                "http://www.swansea.ac.uk/",
                                "http://www.southwales.ac.uk/",
                                "http://www.uwtsd.ac.uk/",
                                "http://www.wales.ac.uk/");
                $count = 0;
                $dir =  get_template_directory_uri().'/assets/img/universities/';
                echo $dir;
                if ($handle = opendir($dir)) {
                  while (false !== ($file = readdir($handle))) {
                    if (preg_match("/'.png$/", $file)) $a[] = $file;
                    elseif (preg_match("/'.jpg$/", $file)) $a[] = $file;
                    elseif (preg_match("/'.jpeg$/", $file)) $a[] = $file;
                  }
                  closedir($handle);
                }
                sort($a);
                foreach ($a as $i) {
                    echo $i; 
                    echo "<div class='col-sm-4'>
                            <a href='". $unis[$count] . "'>
                            <div class='grid-box'>
                                <img class='uni-image' src='" . $dir . $i . "' />
                            ";
                    echo "<h5>" . pathinfo($i, PATHINFO_FILENAME) . "</h5>";
                    echo "</div></a></div>"; //END OF COLUMN 
                    $count++;
                }

这个脚本在不是Wordpress的网站上运行得很好。有什么想法吗?

您应该使用:

$dir = get_stylesheet_directory_uri() . '/assets/img/universities/';

正如在评论中提到的,对于输出客户端的东西,不能使用绝对的服务器路径。

注意:get_stylesheed_directory_uri()将检索当前主题/子主题的样式表目录uri。如果您正在使用子主题,并且需要父主题目录,请使用get_template_directory_uri()。