简体   繁体   English

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

[英]I cannot load jQuery successfully in custom Wordpress theme

Cannot believe I stumbled across this issue again after so many years. 这么多年后,我不敢相信我再次偶然发现了这个问题。 We developed a custom Wordpress theme that needs jQuery (both contact form 7 requires it, looks for it as jQuery and my own custom code looking for it as $). 我们开发了一个需要jQuery的自定义Wordpress主题(联系表7都需要它,将其查找为jQuery,而我自己的自定义代码将其查找为$)。 I can find contradicting code examples everywhere, some say that jQuery is included anyway and that you can simply enqueue it, others that you need a whole function to hold all your scripts etc. 我到处都能找到矛盾的代码示例,有人说无论如何都包括jQuery,您可以将其简单地加入队列,而其他人则需要一个完整的函数来保存所有脚本,等等。

Code tested so far that does not work: (outputs $ is not defined and jQuery is not defined) 到目前为止,已测试的代码无法正常工作:(未定义输出$和未定义jQuery)

Code example 1: 代码示例1:

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

Code example 2: 代码示例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)

Code example 3: 代码示例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'); } add_action('init','td_load_js'); (in fuctions.php, still same errors) (在fuctions.php中,仍然是相同的错误)

So I'm not sure what is wrong at the moment, even leaving the header alone with no script declaration is not working, so I assume I have to load jQuery and the other scripts somehow! 因此,我不确定当前出了什么问题,即使不使用脚本声明也不使用标头也无法正常工作,因此我假设我必须以某种方式加载jQuery和其他脚本! I know I'm not supposed to use jQuery as $ but only as jQuery alone, but I'm leaving my code requesting $ just to look at the error messages until i can get it loaded. 我知道我不应该将jQuery用作$,而只能单独用作jQuery,但是我要离开我的代码,要求$只是查看错误消息,直到可以加载它为止。

UPDATE: Deregistering the local version and running my own definitely works, however I'm still trying to understand why the local version is not loaded automatically, as I cannot see any registration of the jQuery library in other theme's files and neither is it enqueued! 更新:注销本地版本并运行自己的版本肯定可以,但是我仍在尝试理解为什么本地版本没有自动加载的原因,因为我看不到其他主题文件中的jQuery库注册,也没有排队!

both contact form 7 requires it, looks for it as jQuery and my own custom code looking for it as $ 两种联系方式都需要它,将其查找为jQuery,而我自己的自定义代码将其查找为$

jQuery is loaded in noConflict mode in WordPress, change the $ in your code to jQuery . jQuery在WordPress中以noConflict模式加载,将代码中的$更改为jQuery http://api.jquery.com/jQuery.noConflict/ http://api.jquery.com/jQuery.noConflict/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM