【问题标题】:Src file path with php (wordpress) in a javascript filejavascript文件中带有php(wordpress)的src文件路径
【发布时间】:2016-05-04 22:31:05
【问题描述】:

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

<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 或 .. 默认 WP 文件?
  • wordpress文件夹内的外部js
  • 给我 js 处理程序,例如 'wp_enqueue_script('ajax-script', plugins_url('/js/myjquery.js', FILE), array('jquery')) ;' “ajax-script”是处理程序

标签: javascript php wordpress src


【解决方案1】:

在 .js 文件中,我们不能使用 php,因此在 WordPress 中获取像“myscript”这样的值来使用您的脚本“处理程序”。

在你的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";

【讨论】:

    猜你喜欢
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多