如何在WordPress主题中包含jQuery? [英] How to include jQuery in a WordPress theme?

查看:213
本文介绍了如何在WordPress主题中包含jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是WordPress的新手,我正在研究如何将jQuery包含到主题中。

I am pretty new to WordPress and I am figuring out how to include jQuery into a theme.

我在 functions.php <中创建以下函数/ strong>主题:

I create the following function into functions.php theme:

function load_java_scripts() {
    // Load FlexSlider JavaScript that handle the SlideShow:
    wp_enqueue_script('jQuery-js', 'http://code.jquery.com/jquery.js', array(), '1.0', true);
}
add_action('wp_enqueue_scripts', 'load_java_scripts');

所以我认为我可以将其添加为其他一些JavaScript或CSS本地资源,但我不确定关于这个方法,因为在这种情况下, jquery.js 不是本地资源,而是一个在线资源(是同样的事情吗?)

So I think that I can add it as some other JavaScript or CSS local resources but I am not sure about this method because in this case the jquery.js is not a local resource but is an online resource (is it the same thing?)

我也有一些疑惑,因为在线搜索我找到了不同的方法来添加jQuery到我的主题,像这样一个

I also have some doubts because searching online I have found different methods to add jQuery to my theme, like this one.

您能否提供一些有关如何正确完成此任务的信息?

Can you give me some information about how to correctly complete this task?

推荐答案

为什么你没有使用WordPress中找到的jQuery有什么具体原因?

Is there any specific reason why you're not using the jQuery found in WordPress?

如果您需要添加依赖于jQuery的JavaScript文件,可以将jQuery添加为依赖

If you need to add your JavaScript file which depends on jQuery, you can add jQuery as a dependency.

<?php

function my_scripts_method() {
    wp_enqueue_script(
        'custom-script',
        get_stylesheet_directory_uri() . '/js/custom_script.js', #your JS file
        array( 'jquery' ) #dependencies
    );
}

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

?>

请注意,WordPress在没有冲突包装器。所以你的代码应该是这样的:

Note that WordPress loads jQuery in no conflict wrappers. so your code should be like:

jQuery(document).ready(function($) {
    // Inside of this function, $() will work as an alias for jQuery()
    // and other libraries also using $ will not be accessible under this shortcut
});

这篇关于如何在WordPress主题中包含jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆