将jQuery添加到Underscores(_s)WordPress主题的最佳方法是什么?

时间:2014-07-28 10:45:35

标签: jquery wordpress wordpress-theming

这似乎是一件基本的事情,但我不确定如何将jQuery (使用CDN)添加到 Underscores < strong> WordPress入门主题。我做了一些搜索,但没有找到任何东西,所以我希望有人可以帮我找到答案?

提前谢谢!

3 个答案:

答案 0 :(得分:11)

最好的方法是在Underscores创建的主题functions.php文件中使用Wordpress函数wp_enqueue_script()

找到函数<THEME_NAME>_scripts(其中<THEME_NAME>是从Underscores网站生成时的主题名称)并在<THEME_NAME>_scripts函数下添加以下代码(在任何其他wp_enqueue_script之前) )

// deregister default jQuery included with Wordpress
wp_deregister_script( 'jquery' );

$jquery_cdn = '//ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js';
wp_enqueue_script( 'jquery', $jquery_cdn, array(), '2.2.4', true );

答案 1 :(得分:2)

Underscores不会依赖于jquery的任何javascript。因此,当使用simple-auth-token/authenticators/token对javascript文件进行排队时,默认安装不会在WordPress中加载默认jquery,而不会将其设置为依赖项。

将bootstrap加载到下划线并从WordPress加载jquery的示例是

wp_enqueue_script

您可以使用WordPress中注册的CDN的jquery文件执行相同的操作

答案 2 :(得分:0)

您可以在 header.php footer.php 中使用此代码段:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

这使用Google托管的jQuery。

注意: WordPress附带jQuery,所以如果你使用Underscores作为WordPress主题,那么jQuery将已经入队。

参考: https://developers.google.com/speed/libraries/devguide#jquery

相关问题