繁体   English   中英

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

暂无
暂无

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

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