带有 Javascript 的 Wordpress 根 URL [英] Wordpress root URL with Javascript
问题描述
我在我的 Wordpress 网站上使用这个脚本:JQuery 插件:随机图片
I am using this script on my Wordpress website: JQuery Plugin: Random Image
我遇到的问题是我不知道在路径部分放置什么样的代码,以便 Wordpress 能够从图像文件夹中获取图像./images 是不够的,因为它只会给我 www.mydomain.com/images/image.jpg,显然这不是使用 Wordpress 时的完整 URL.
The problem I have is that I don't know what sort of code to put in the path section so that Wordpress would be able to fetch the images from the images folder. /images is not enough, since it'll just give me www.mydomain.com/images/image.jpg, and obviously that is not the whole URL when using Wordpress.
我试过这个:js脚本文件中的wordpress路径网址
但是我无法让它工作.每当我放置
I can't get it to work however. Whenever I place
var templateUrl = '<?= get_bloginfo("template_url"); ?>';
在我的文件中,我收到一个错误
in my file, I just get an error
%3C?=%20get_bloginfo(%22template_url%22);%20?%3E/images/image.jpg
我做错了什么?
推荐答案
好的,我通过反复试验得到了它,我会在这里列出我做错的事情,以防其他人遇到同样的问题:
Ok, I got it working through trial and error and I will list here things that I did wrong, just in case someone else is having the same problem:
我将 var templateUrl 代码放在单独的 javascript 标签中.后来我把它移到了 $(document).ready(function() {
I put the var templateUrl code in separate javascript tags. Later on I moved it just above $(document).ready(function() {
我将 templateUrl 放在实际的 .js 文件中.后来我把它移到了:$('.shuffle').randomImage({path: templateUrl +/images/});
I put templateUrl in the actual .js file. I later on moved it to: $('.shuffle').randomImage({path: templateUrl + /images/});
最终结果如下:
<script type="text/javascript">
var templateUrl = '<?php bloginfo('template_directory'); ?>';
$(document).ready(function() {
$('.shuffle').randomImage({path: templateUrl + /images/});
});
</script>
脚本现在完美运行!感谢大家的帮助!:)
The script is now working perfectly! Thanks to everyone for help! :)
这篇关于带有 Javascript 的 Wordpress 根 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!