我对 php 相当陌生,所以请原谅我缺乏知识。
目前我正在尝试向子主题添加一个小的 javascript 文件。我已将以下代码添加到 child 的functions.php 文件中。
if ( is_page_template( 'page-templates/edit-profile.php' ) ) {
function add_form_unlock() {
wp_enqueue_script(
'form_unlock',
$get_stylesheet_directory_uri() . '/js/form_unlock.js',
array( 'jquery'),
'1.1',
true
);
}
add_action( 'wp_enqueue_scripts', 'add_form_unlock' );
}
我的 JavaScript 是
var pass1 = "QuL7eD";
function check(){
var pass2 = document.getElementById("password").value;
if (pass1 == pass2) {
document.getElementById("button").disabled = false;
document.getElementById("test").disabled = false;
}
}
我已经成功地在 test.html 文件中运行了 javascript,很明显它只是没有在 WordPress 模板中运行。
任何帮助都会很棒!
最佳答案
不要将 WordPress Hook /过滤器包装在 IF 语句内。 if 语句返回 false,因为在计算 function.php 文件时您不在模板内。
if 语句应该位于 add_form_unlock 函数内部,如下所示:
function add_form_unlock() {
if ( is_page_template( 'page-templates/edit-profile.php' ) ) {
wp_enqueue_script(
'form_unlock',
get_stylesheet_directory_uri() . '/js/form_unlock.js',
array( 'jquery'),
'1.1',
true
);
}
}
add_action( 'wp_enqueue_scripts', 'add_form_unlock' );
关于javascript - 在functions.php中导入javascript不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072386/