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


Src file path with php (wordpress) in a javascript file

由于某些原因,用wordpress制作的模板的正常src路径不起作用。它嵌入在一个连接到我的footer.php的javascript中。现在我的问题是:有可能在一个带有php片段的javascript中调用一个带有src的图像吗?这是我到目前为止的代码:

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";

我正在尝试添加一个类似的php片段:

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

实现这样的目标:

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";

我试图构建一个名为:的var

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

我添加了src,但我没有工作。。有线索吗?

在.js文件中,我们不能使用php,所以在WordPress中,像这样获取值"myscript"来使用脚本"handler"。

在您的functions.php文件中

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

在js文件中,像这样使用alert只是您的信息。

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";