jquery - 如何使 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.php
,index.php
当我在之前放置脚本标签wp_head()
并加载页面时,它之前还有 15 个脚本标签,我认为它们与我的插件有关
所以我能做些什么来让 Jquery 真正在一切之前加载,因为编辑第三方插件真的很令人沮丧和耗时
更新
解决方案
Cameron,您可以通过在 header.php 或 functions.php 文件中加入 jQuery 来使用它。下面是几篇文章,一步一步来:
https://digwp.com/2009/06/include-jquery-in-wordpress-the-right-way/
或者
更新:
尝试将<head>
header.php 文件的部分更改为:
<head>
<?php wp_enqueue_script("jquery"); ?>
<meta charset="<php bloginfo('charset'); ?>" />
<?php wp_head(); ?>
</head>
推荐阅读
- javascript - 在 Laravel Mix webpack.mix 中找到同时包含 React + TypeScript 的正确解决方案?
- node.js - 节点串行端口:应用超时时无法使用 RxJS Observable 处理传入数据
- r - 在 lda 分析期间我的数据准备有问题吗?
- excel - 需要带有临时变量的公式
- python - 在 pandas 中,如何在单个单元格中设置数字格式?
- mysql - 将三个表与两个动态值组合在一起
- relational-database - 是否可以使用关系演算来表达非空约束?
- php - 读取 XML 时嵌套 foreach(在 PHP 中)
- php - 贝宝不支持的货币和费用php
- javascript - 如何让鼠标悬停在我想要的范围内工作?