CDN 链接失效
CDN links not working
我在 Google 上读到 CDN 链接更适合使用,因为它速度快且有额外的安全层。我是 WordPress 开发新手。我将 CDN 链接放在一个 JS 文件中并排队。但是,它在我的网站上没有生效。
自定义-js.js
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
functions.php
<?php
function load_css_js() {
wp_register_style( 'child-css', get_stylesheet_directory_uri() . '/style.css', false, NULL, 'all' );
wp_enqueue_style( 'child-css' );
wp_register_script( 'child-js', get_stylesheet_directory_uri() . '/js/custom-js.js', array( 'jquery' ), NULL, false );
wp_enqueue_script( 'child-js' );
}
add_action( 'wp_enqueue_scripts', 'load_css_js' );
?>
1) 首先。您是否正在尝试在 尚未完全使用 Bootstrap 的父主题之上构建子主题?
2) 是否设置了子主题 style.css(请参阅下面的子主题 link)?
加载中Scripts/Stylesheets:
您的 functions.php
文件中应该包含与这些函数类似的内容。
一般来说,您不需要加载 jQuery,但 default 已经准备就绪(请参阅 Codex:WordPress 包含和注册的默认脚本) 所以用 DEV Tools 检查页面是否正在加载(除非你想使用另一个 CDNs 库,然后你需要禁用默认的,这样你就不会 jQuery 加载两次。
Codex: Function Reference/wp enqueue script
Codex: Function Reference/wp enqueue style
入队 JS
function my_scripts() {
wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', '2.1.3', true );
wp_enqueue_script( 'bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array('jquery'), '3.3.5', true );
}
add_action('wp_enqueue_scripts', 'my_scripts');
排队CSS
function my_styles() {
wp_enqueue_style( 'bootstrap-css', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css' );
wp_enqueue_style( 'bootstrap-css', get_stylesheet_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'my_styles');
我在 Google 上读到 CDN 链接更适合使用,因为它速度快且有额外的安全层。我是 WordPress 开发新手。我将 CDN 链接放在一个 JS 文件中并排队。但是,它在我的网站上没有生效。
自定义-js.js
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
functions.php
<?php
function load_css_js() {
wp_register_style( 'child-css', get_stylesheet_directory_uri() . '/style.css', false, NULL, 'all' );
wp_enqueue_style( 'child-css' );
wp_register_script( 'child-js', get_stylesheet_directory_uri() . '/js/custom-js.js', array( 'jquery' ), NULL, false );
wp_enqueue_script( 'child-js' );
}
add_action( 'wp_enqueue_scripts', 'load_css_js' );
?>
1) 首先。您是否正在尝试在 尚未完全使用 Bootstrap 的父主题之上构建子主题?
2) 是否设置了子主题 style.css(请参阅下面的子主题 link)?
加载中Scripts/Stylesheets:
您的 functions.php
文件中应该包含与这些函数类似的内容。
一般来说,您不需要加载 jQuery,但 default 已经准备就绪(请参阅 Codex:WordPress 包含和注册的默认脚本) 所以用 DEV Tools 检查页面是否正在加载(除非你想使用另一个 CDNs 库,然后你需要禁用默认的,这样你就不会 jQuery 加载两次。
Codex: Function Reference/wp enqueue script
Codex: Function Reference/wp enqueue style
入队 JS
function my_scripts() {
wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', '2.1.3', true );
wp_enqueue_script( 'bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array('jquery'), '3.3.5', true );
}
add_action('wp_enqueue_scripts', 'my_scripts');
排队CSS
function my_styles() {
wp_enqueue_style( 'bootstrap-css', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css' );
wp_enqueue_style( 'bootstrap-css', get_stylesheet_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'my_styles');