【问题标题】:My WordPress don't display the images that I've hardcoded我的 WordPress 不显示我硬编码的图像
【发布时间】:2016-05-20 21:18:54
【问题描述】:

我正在用 WordPress 编写一个网站,我想在课程名称前显示一个书签星标。

问题是代码工作正常,我可以看到它转换到图像所在的正确位置,但似乎找不到图像。

我是否需要做任何配置才能显示此图像或我做错了什么?

代码:

$estrela = get_stylesheet_directory() . '/images/estrela.png';
$estrelaFavorito = get_stylesheet_directory() . '/images/estrela-favorito.png';

if ($isFavorito) {
    $img = $estrelaFavorito;
} else {
    $img = $estrela;
}

if (is_user_logged_in()) { ?>
    <div class="add-remove-bookmark" onclick="addToBookmark(<?php echo $user_id ?>, 'course', <?php echo $course_id ?>)" >
        <img src="<?php echo $img; ?>" alt="favoritos" width="20" height="20"/>
    </div>
<?php }

结果:

<div class="add-remove-bookmark" onclick="addToBookmark(x, x, x)">
    <img src="/home/xxx/www/wp-content/themes/wplms_child/images/estrela-favorito.png" alt="favoritos" width="20" height="20">
</div>

编辑:

如果我更改为下面的代码,它可以工作,但不是真正的最佳实践:

$estrela = 'https://www.xxx.com.br/wp-content/themes/wplms_child/images/estrela.png';
$estrelaFavorito = 'https://www.xxx.com.br/wp-content/themes/wplms_child/images/estrela-favorito.png';

解决方案:

我已经为下面的代码替换了变量$estrela$estrelaFavorito,现在它可以工作了!

$estrela = get_stylesheet_directory_uri() . '/images/estrela.png';
$estrelaFavorito = get_stylesheet_directory_uri() . '/images/estrela-favorito.png';

【问题讨论】:

  • 尝试在浏览器中打开你生成的路径进行验证
  • /home/xxx/www/wp-content/themes/wplms_child/images/estrela-favorito.png 重定向到另一个页面?!
  • 您在本地机器上开发?任何.htaccess 文件?或者在 vHost/您的网络服务器中是否定义了任何重定向?
  • 我明白了...为什么不使用相对路径?
  • 是的,这是相对路径

标签: php html wordpress


【解决方案1】:

替换:

//Returns an absolute server path
get_stylesheet_directory()

通过

// return theme directory url 
get_stylesheet_directory_uri()

https://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri

【讨论】:

    猜你喜欢
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    • 2018-01-01
    • 1970-01-01
    相关资源
    最近更新 更多