如何在 WordPress 主题中包含 jQuery?

     2023-02-24     116

关键词:

【中文标题】如何在 WordPress 主题中包含 jQuery?【英文标题】:How to include jQuery in a WordPress theme? 【发布时间】:2014-02-10 23:04:31 【问题描述】:

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

我在 functions.php 主题中创建了以下函数:

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 不是本地资源,而是在线资源(是一样的吗?)

我也有一些疑问,因为在网上搜索我发现了不同的方法来将 jQuery 添加到我的主题中,比如one。

你能给我一些关于如何正确完成这项任务的信息吗?

【问题讨论】:

为什么要手动包含 jquery?不是都已经包含了吗? 【参考方案1】:

您没有使用 WordPress 中的 jQuery 有什么具体原因吗?

如果您需要添加依赖于 jQuery 的 JavaScript 文件,您可以将 jQuery 添加为 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 会在 no conflict wrappers 中加载 jQuery。所以你的代码应该是这样的:

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
);

【讨论】:

谢谢@RRikesh,这就是问题所在。我忽略了 jQuery 加载。我不知道无冲突包装器。由于某种原因,我似乎无法将您的答案标记为解决方案。【参考方案2】:

由于 WP 已经自带 jQuery,我会简单地为你的主题加载它,像这样将它添加到你的 functions.php 中

function load_scripts()
    //Load scripts:
    wp_enqueue_script('jquery'); # Loading the WordPress bundled jQuery version.
    //may add more scripts to load like jquery-ui

add_action('wp_enqueue_scripts', 'load_scripts');

有几种方法可以将 jQuery 包含到主题中。我总是使用 WP 捆绑版本,我觉得非常简单。

【讨论】:

【参考方案3】:

在 wordpress 中不需要自定义 Jquery。 将依赖项添加为“jquery”,它将自动加载。

【讨论】:

【参考方案4】:

试试这个,

<?php
    function load_external_jQuery() 
        wp_deregister_script( 'jquery' ); // deregisters the default WordPress jQuery 
        $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'; // the URL to check against  
        $test_url = @fopen($url,'r'); // test parameters  
        if( $test_url !== false )  // test if the URL exists if exists then register the external file  
            wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js');
        
        else// register the local file 
            wp_register_script('jquery', get_template_directory_uri().'/js/jquery.js', __FILE__, false, '1.7.2', true);  
        
        wp_enqueue_script('jquery'); // enqueue the jquery here
    
    add_action('wp_enqueue_scripts', 'load_local_jQuery'); // initiate the function 
?>

【讨论】:

【参考方案5】:

您可以使用以下方法来包含 jQuery:

wp_enqueue_script( 'jquery' );  

wp_enqueue_script( 'load-js-validate', 'foldername/jquery.js' );

Directly add in header file.<script type="text/javascript" src="<?php bloginfo('template_directory'); ?>/js/jquery.js"></script>

function js_scripts() 
wp_enqueue_script( 'jquery', get_template_directory_uri() . '/js/example.js');


add_action( 'wp_enqueue_scripts', 'js_scripts' ); // add this in function file

【讨论】:

在主题wordpress中包含jquery

if(!is_admin())wp_deregister_script('jquery');wp_register_script('jquery',("http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"),false,'1.4.2');wp_enqueue_script('jquery'); 查看详情

在wordpress主题中包含jquery

if(!is_admin()){ wp_deregister_script('jquery'); wp_register_script('jquery',("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"),false,'1.3.2'); wp_enqueue_script('jquery'); } 查看详情

如何在自定义 .php 文件中包含 WordPress 函数?

】如何在自定义.php文件中包含WordPress函数?【英文标题】:HowtoincludeWordPressfunctionsincustom.phpfile?【发布时间】:2013-02-2415:25:30【问题描述】:详细说明:我的主题(构造函数)下有一个名为报告的目录。这些包含.php文件,这些... 查看详情

在 functions.php (WordPress) 中包含 ACF

】在functions.php(WordPress)中包含ACF【英文标题】:IncludingACFinfunctions.php(WordPress)【发布时间】:2013-10-2416:49:10【问题描述】:我正在制作第一次在主题forrest上出售的主题。我在开发过程中使用ACF作为标准插件,以确保一切正常。我... 查看详情

在 WordPress 网站中包含汉堡菜单

】在WordPress网站中包含汉堡菜单【英文标题】:IncludingHamburgerMenuInWordPressSite【发布时间】:2017-04-0620:51:08【问题描述】:http://thehamburgercollection.com/shop/我正在尝试创建一个汉堡菜单,其中菜单项直接下拉到汉堡图标下方(例如... 查看详情

如何在 Wordpress 自定义帖子类型查询中包含分页

】如何在Wordpress自定义帖子类型查询中包含分页【英文标题】:HowtoincludepaginationinaWordpressCustomPostTypeQuery【发布时间】:2017-06-0322:47:34【问题描述】:我有以下代码:<?php$the_query=newWP_Query(\'posts_per_page=30&post_type=phcl\');?><... 查看详情

如何在我的自定义主题中包含自定义 js 文件?

】如何在我的自定义主题中包含自定义js文件?【英文标题】:Howtoincludecustomjsfileinmycustomtheme?【发布时间】:2021-01-0520:44:42【问题描述】:使用OroCommercev4.1.8(最新稳定版)。我创建了我的自定义主题(从“默认”主题扩展),... 查看详情

如何在 s-s-rS 订阅的主题行中包含报告参数?

】如何在s-s-rS订阅的主题行中包含报告参数?【英文标题】:HowdoIincludereportparametersinans-s-rSsubscription\'ssubjectline?【发布时间】:2014-07-0715:56:33【问题描述】:我有一份显示指定日期数据的报告。该报告每天运行,并通过电子邮件... 查看详情

在 WordPress 中包含一个 jQuery 插件

】在WordPress中包含一个jQuery插件【英文标题】:IncludingajQueryplugininsideofWordPress【发布时间】:2013-08-3002:37:17【问题描述】:我目前正在将我的HTML转换为WordPress网站。该站点使用\'inview\'jQuery插件。我似乎无法让它在WordPress中运行... 查看详情

Wordpress:在 url 中包含语言变量

】Wordpress:在url中包含语言变量【英文标题】:Wordpress:includelanguagevariableinurl【发布时间】:2015-09-2313:59:03【问题描述】:我一直在为wordpress开发一个可以完全运行的语言插件。现在唯一缺少的是url重写。我一直在查看有关***的... 查看详情

WordPress 是不是在管理页面中包含 jQuery?

】WordPress是不是在管理页面中包含jQuery?【英文标题】:DoesWordPressincludejQueryinadminpages?WordPress是否在管理页面中包含jQuery?【发布时间】:2015-02-0608:17:54【问题描述】:我正在为WordPress开发一个插件。对于插件的设置页面,假设... 查看详情

php在wordpress中包含jquery(代码片段)

查看详情

php在wordpress插件中包含js(代码片段)

查看详情

在 GraphQL Wordpress 查询搜索中包含 mediaItems

】在GraphQLWordpress查询搜索中包含mediaItems【英文标题】:IncludemediaItemsinGraphQLWordpressquerysearch【发布时间】:2020-09-1114:30:14【问题描述】:我有一个无头WordPress安装,并使用React前端通过以下搜索查询来查询帖子:posts(where:search:$se... 查看详情

在 Wordpress 中包含一个 github jquery 插件

】在Wordpress中包含一个githubjquery插件【英文标题】:IncludingagithubjqueryplugininWordpress【发布时间】:2016-02-0504:11:13【问题描述】:我正在尝试在我的wordpress网站上包含来自github(https://github.com/rendro/countdown)正确方式的插件。经过数... 查看详情

我需要在生产就绪项目中包含 node_modules 吗?

...间】:2021-10-0403:09:10【问题描述】:如果我正在构建一个WordPress主题,我需要在完成的主题中包含node_modules文件夹还是仅用于开发?运行我的gitcommit时,我应该忽略node_modules文件夹吗?【问题讨 查看详情

WordPress 重定向插件,用于在目标 URL 中包含查询参数

】WordPress重定向插件,用于在目标URL中包含查询参数【英文标题】:WordPressRedirectionplugintoincludequeryparametersintargetURL【发布时间】:2018-08-2007:57:57【问题描述】:我正在使用WordPress重定向插件将旧登录页面重定向到新域。我能够... 查看详情

在wordpress中包含jquery

if(!is_admin()){wp_deregister_script('jquery');wp_register_script('jquery',("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"),false,'1.3.2');wp_enqueue_script('jquery');} 查看详情