douzhou7124 2017-09-02 14:14
浏览 98

get_stylesheet_directory_uri()和get_template_directory_uri()没有返回“http”

I've made a local copy using xampp of my wordpress website. The theme was written by me. Locally, every image link using get_template_directory_uri() or get_stylesheet_directory_uri() is broken because it is missing the "http" prefix. If I add it manually, then the image is displayed correctly.

This is the code: <?php echo get_stylesheet_directory_uri() . '/images/myimage.jpg' ?>

the above functions online generate:
http://www.mywebsite.it/wp-content/themes/mytheme/images/myimage.jpg"
locally on xampp they generate:
localhost/myfolder/wp-content/themes/mytheme/images/myimage.jpg
and the image is not displayed. If I manually add http:// before localhost it works.

Interestingly enough, the function get_template_directory_uri() is also used in functions.php to enqueue some stylesheets, and there it generates the correct url beginning with http even if locally.

I don't know hot to fix this, I don't want to change every link here locally, and having to remember to fix it back before putting it online. Sorry for my english.

Edit: I was able to automatically add "http" by escaping the url, with this code
<?php echo esc_url(get_stylesheet_directory_uri() . '/images/myimage.jpg') ?>
This fixes the problem locally, and doesn't affect the output online, therefore I can sync the code like this... still, I don't know why it behaved like this.

  • 写回答

1条回答 默认 最新

  • douzhuang1900 2017-09-03 07:05
    关注

    Please check WordPress Address (URL) and Site Address (URL) in Settings

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入