简体   繁体   English

javascript文件中带有php(wordpress)的src文件路径

[英]Src file path with php (wordpress) in a javascript file

For some reason the normal src path of my template made with wordpress doesn't work. 由于某些原因,我用wordpress制作的模板的正常src路径不起作用。 It is embedded in a javascript connect to my footer.php. 它嵌入到我的footer.php的javascript中。 Now my question is: is it possible to call an image with src inside a javascript with a php snippet? 现在我的问题是:是否可以在带有php片段的javascript中调用带有src的图像? Here is my code so far: 到目前为止,这是我的代码:

var d = new Date();

document.getElementById("copyright").innerHTML = "&copy;" + " " + (d.getFullYear()) + " " + "All Rights Reserved" + " " + " " +"<img src='wp-content/themes/wpboot/images/maple.svg' alt='Canadian maple leaf' width='14'>" + " " + "We Are A Canadian Company";

I'm trying to add a php snippet like that: 我正在尝试添加这样的php代码段:

<img src="<?php bloginfo('template_directory'); ?>/images/maple.svg" width="60" alt="Maple">

To achieve something like that: 为了达到这样的目的:

document.getElementById("copyright").innerHTML = "&copy;" + " " + (d.getFullYear()) + " " + "All Rights Reserved" + " " + " " +"<img src='<?php bloginfo('template_directory'); ?>/images/maple.svg' alt='Canadian maple leaf' width='14'>" + " " + "We Are A Canadian Company";

I tried to build a var called: 我试图建立一个名为:

var template = "<?php bloginfo('template_directory'); ?>/";

And I added to the src but I didn't work.. any clue? 并且我添加到src中,但是我没有工作..有什么线索吗?

In .js file we can't used php so in WordPress get values as like this "myscript" to used your script "handler". 在.js文件中,我们无法使用php,因此在WordPress中,可以像“ myscript”这样的值来使用脚本“ handler”。

in your functions.php file 在您的functions.php文件中

wp_enqueue_script('myscript',get_template_directory_uri().'/js/myscript.js',array('jquery'));
wp_localize_script( 'myscript', 'mycustomurl',  get_template_directory_uri() );

in js file use as like this alert just your info. 在js文件中使用,就像这样的警报只是您的信息。

alert(mycustomurl);
document.getElementById("copyright").innerHTML = "&copy;" + " " + (d.getFullYear()) + " " + "All Rights Reserved" + " " + " " +"<img src='"+mycustomurl+"/images/maple.svg' alt='Canadian maple leaf' width='14'>" + " " + "We Are A Canadian Company";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM