首页 > 解决方案 > 如何使 jQuery 加载为 WordPress 标头中的第一个脚本?

问题描述

我的网站和插件在标签之间插入了一些 jquery 代码,HTML并将依赖于 jquery 的插件作为第一个脚本标签,所以无论我做什么将 jquery 作为第一个脚本插入到 head 部分,我都无法让它工作

我试过了

function use_jquery_from_google () {
    if (is_admin()) {
        return;
    }

    global $wp_scripts;
    if (isset($wp_scripts->registered['jquery']->ver)) {
        $ver = $wp_scripts->registered['jquery']->ver;
    } else {
        $ver = '3.1.4';
    }

    wp_deregister_script('jquery');
    wp_register_script('jquery', "//ajax.googleapis.com/ajax/libs/jquery/$ver/jquery.min.js", false, $ver);
}

还放了 JQuery CND 脚本标签

<script
  src="https://code.jquery.com/jquery-3.4.1.min.js"
  integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
  crossorigin="anonymous"></script>

几乎所有地方,包括直接进入主题header.phpindex.php当我在之前放置脚本标签wp_head()并加载页面时,它之前还有 15 个脚本标签,我认为它们与我的插件有关

所以我能做些什么来让 Jquery 真正在一切之前加载,因为编辑第三方插件真的很令人沮丧和耗时

更新

在此处输入图像描述

在此处输入图像描述

标签: jqueryhtmlwordpressheader

解决方案


Cameron,您可以通过在 header.php 或 functions.php 文件中加入 jQuery 来使用它。下面是几篇文章,一步一步来:

https://digwp.com/2009/06/include-jquery-in-wordpress-the-right-way/

或者

如何在 Wordpress 中安装 jQuery?

更新:

尝试将<head>header.php 文件的部分更改为:

<head>
<?php wp_enqueue_script("jquery"); ?>
<meta charset="<php bloginfo('charset'); ?>" />
<?php wp_head(); ?>
</head>

推荐阅读