【问题标题】:Images not loading when path is correct PHP, Wordpress路径正确时无法加载图像PHP,Wordpress
【发布时间】:2014-12-17 20:17:26
【问题描述】:

我有一个加载一些图像的脚本,我有一个 $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 的网站上完美运行。有什么想法吗?

【问题讨论】:

  • 您是否将此路径发送到浏览器?您不能在客户端使用绝对服务器路径。
  • 请检查img标签的来源并在浏览器上运行。

标签: php html wordpress image


【解决方案1】:

你应该使用:

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

正如 cmets 中所提到的,您不能对正在输出客户端的事物使用绝对服务器路径。

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

【讨论】:

  • 这为我提供了一个合适的网址www.mysite.com/assets/img/universities/,但图像仍然无法加载:/
  • ...那应该给你www.mysite.com/wp-content/themes/roots-master/assets/img/universities/
  • 我现在把它作为 URL `uniswales.ac.uk/wp/wp-content/themes/roots-master/assets/img/…` 可以在浏览器中使用。只是不在 PHP 文件中 :(
  • 如果你echo $dir;,上面的结果是什么?
  • 我正在用完整的脚本更新我的问题。
猜你喜欢
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-07
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多