我无法在自定义Wordpress主题中成功加载jQuery

I cannot load jQuery successfully in custom Wordpress theme

本文关键字:成功 加载 jQuery Wordpress 自定义      更新时间:2024-02-06

真不敢相信这么多年后我又偶然发现了这个问题。我们开发了一个需要jQuery的自定义Wordpress主题(联系人表单7都需要它,将其作为jQuery查找,我自己的自定义代码将其作为$查找)。我到处都能找到矛盾的代码示例,有些说jQuery无论如何都包含在内,你可以简单地将其排队,另一些说你需要一个完整的函数来保存所有的脚本等。

到目前为止测试的代码不起作用:(输出$未定义,jQuery未定义)

代码示例1:

wp_enqueue_script( 'jquery' ); (does not work, get above error, in header.php)

代码示例2:

function loadScripts() {
    wp_enqueue_script( 'jquery' );
wp_register_script('jqueryCycle',get_template_directory_uri().'/js/jquery.cycle.js', 'jquery');
    wp_enqueue_script( 'jqueryCycle' );                 
    wp_register_script('javascript', get_template_directory_uri().'/js/javascript.js', 'jquery');
    wp_enqueue_script( 'javascript' );
wp_register_script('jqueryCookie', get_template_directory_uri().'/js/jquery.cookie.js', 'jquery');
    wp_enqueue_script( 'jqueryCookie' );
    wp_register_script('jqueryCookiecuttr', get_template_directory_uri().'/js/jquery.cookiecuttr.js', 'jquery' );
    wp_enqueue_script( 'jqueryCookiecuttr' );
}
add_action( 'wp_enqueue_scripts', 'loadScripts' );  (code i got from older theme, same errors)

代码示例3:

function td_load_js() {
    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'javascript', get_template_directory_uri() . '/js/javascript.js', array( 'jquery' ), 1, false);

}add_action('init','td_load_js');(在函数.php中,仍然存在相同的错误)

因此,我不确定目前出了什么问题,即使不使用脚本声明而单独保留标头也不起作用,所以我认为我必须以某种方式加载jQuery和其他脚本!我知道我不应该将jQuery用作$,而只能单独用作jQuery,但我留下请求$的代码只是为了查看错误消息,直到我可以加载它。

更新:取消注册本地版本并运行我自己的版本肯定有效,但我仍在努力理解为什么本地版本没有自动加载,因为我在其他主题的文件中看不到jQuery库的任何注册,也没有排队!

联系人表单7都需要它,以jQuery和我自己的身份查找自定义代码查找它作为$

jQuery在WordPress中以noConflict模式加载,请将代码中的$更改为jQuery。http://api.jquery.com/jQuery.noConflict/