【发布时间】:2017-05-07 01:01:31
【问题描述】:
我想用wp_enqueue_script调用一个js文件。
我使用get_template_directory(),就像这样:
$myfile = wp_normalize_path(get_template_directory().'/js/script.js');
$myversion = filemtime($myfile);
wp_enqueue_script('myscript', $myfile , array( 'jquery' ), $myversion, true );
这适用于服务器:如果我回显$myfile,它会返回一个真实路径,如/home/public_html/folder/wp-content/themes/mytwentysixteen/js/script.js,而在网页上它会正确返回文件的绝对路径。
(请注意,如果我使用 get_template_directory_uri,上述内容 - 至少 filemtime 部分会失败。)
在我的本地 xampp 安装(Windows 机器)上,这不起作用。
如果我做echo $myfile,它会返回正确的本地路径:
D:/path/to/folder/wp-content/themes/mytwentysixteen/js/script.js
但是,在wp_enqueue_script 之后,它在网页上返回如下内容:
http://localhost/folderD:pathtofolder/wp-content/themes/mytwentysixteen/js/script.js
并且页面无法检索脚本。 这似乎是 localhost 上的 home url 和本地 windows 路径之间的奇怪结合。
wp_normalize_path 似乎没有帮助。
【问题讨论】: