【问题标题】:Adding custom javascript to functions.php in wordpress在wordpress中将自定义javascript添加到functions.php
【发布时间】:2018-01-01 20:11:48
【问题描述】:

我正在尝试向我的 WordPress 网站添加一个 javascript 文件,但该文件无法加载。这是我的 functions.php 文件中的代码。

function add_custom_script() {

wp_register_script('custom_scripts', get_template_directory_uri().'custom.js', array(),'', true);

wp_enqueue_script('custom_scripts');
}

add_action( 'wp_enqueue_scripts', 'add_custom_script' );

谢谢

【问题讨论】:

  • 'custom.js' 上添加/,因为get_template_directory_uri() 返回的模板URL 没有尾部斜杠。

标签: javascript wordpress templates


【解决方案1】:

您可以只使用 wp_enqueue_script(),正如 Mithc 所说,get_template_directory_uri() 不会添加尾部斜杠。

尝试使用:

function add_custom_script() {

    wp_enqueue_script('custom_scripts', get_template_directory_uri().'/custom.js', array(),'', true);
}

add_action( 'wp_enqueue_scripts', 'add_custom_script' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 2011-03-20
    • 2014-06-05
    • 1970-01-01
    相关资源
    最近更新 更多