Wordpress JS文件加载两次

问题描述 投票:0回答:2

我想在我的WordPress安装中包含Bootstrap 4。这就是我在儿童主题中将其包含在我的functions.php中的方式。

// Bootstrap 4
function bootstrap() {
    wp_enqueue_style('bootstrap-css', get_stylesheet_directory_uri() . '/css/bootstrap.min.css' );
    wp_enqueue_script( 'jquery-slim', 'https://code.jquery.com/jquery-3.2.1.slim.min.js', array(), '3.2.1', true );
    wp_enqueue_script( 'popper', 'https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js', array('jquery-slim'), '4.0.0', true );
    wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js', array('jquery-slim'), '4.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'bootstrap');

当我在pingdom或GTMetrix上测试我的网站时,他们说它被加载了两次。为什么?

The following resources have identical contents, but are served from different URLs. Serve these resources from a consistent URL to save 1 request(s) and 68.0KiB.

https://code.jquery.com/jquery-3.2.1.slim.min.js
https://code.jquery.com/jquery-3.2.1.slim.min.js?ver=3.2.1

https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js
https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js?ver=4.0.0

父主题未加载任何这些文件。但是为什么例如popper.js没有加载两次?

javascript jquery wordpress
2个回答
1
投票

好吧,WP本身包括jquery和bootstrap,如果你想要你的特定的那些你应该取消注册核心/默认的,然后注册你的(没有jquery和bootstrap它不会做太多):

wp_deregister_script('jquery'); //because its a Core-Registered Script
wp_register_script('jquery', 'THE PATH TO THE FILE', array(), '', FALSE);

答案在这里:https://wordpress.stackexchange.com/a/284620


0
投票

Javascript和CSS文件的双重加载可以是浏览器扩展的结果。

因此,在向functions.php文件中添加大量挂钩之前,请尝试逐个禁用浏览器扩展,以查看是否可以解决问题。

定位特定的开发扩展,例如CSS Auto Reload,这将对您的页面加载产生这种影响。

我知道这是一个老线程,但我认为在StackOveflow上给出答案永远不会太晚,希望这会对某人有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.