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

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

由于某种原因,我用 wordpress 制作的模板的正常 src 路径不起作用。它嵌入在 javascript 连接到我的 footer.php 中。现在我的问题是:是否可以使用 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 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(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";