将自定义javascript添加到wordpress中的functions.php

问题描述 投票:0回答:1

我正在尝试将javascript文件添加到我的WordPress网站,但该文件无法加载。这是我的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' );

谢谢

javascript wordpress templates
1个回答
0
投票

你可以简单地使用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' );
© www.soinside.com 2019 - 2024. All rights reserved.