【问题标题】:CDN links not workingCDN 链接不起作用
【发布时间】:2015-08-21 08:33:01
【问题描述】:

我在 Google 上读到 CDN 链接更适合使用,因为它具有速度和额外的安全层。我是 WordPress 开发的新手。我将 CDN 链接放在一个 JS 文件中并入队。但是,它没有在我的网站上生效。

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

?>

【问题讨论】:

  • 那么生成的 html 中会显示什么?
  • @MarcB 只是 HTML,没有花哨的 Bootstrap 元素。没有颜色,没有整洁的按钮,等等……很普通
  • 你是不是把这些链接加到了一个js文件里?它们属于您的 html
  • 请在 SSCC 示例中附加生成的 HTML 和您的 JS 文件。见sscce.orgjsfiddle.net
  • "以及额外的安全层" --- 怎么样?实际上恰恰相反。

标签: javascript php wordpress twitter-bootstrap cdn


【解决方案1】:

1) 先于其他。您是否尝试在 不完全使用 Bootstrap 的 Parent 主题之上构建 Child 主题?

2) 您是否设置了子主题 style.css(请参阅下面的子主题链接)?

加载脚本/样式表:

您的functions.php 文件中应该有类似于这些函数的内容。

一般来说,您不需要加载 jQuery,default 已经准备好了(请参阅 Codex:WordPress 包含和注册的默认脚本),因此请使用 DEV Tools 检查页面看看它是否正在加载(除非你想使用另一个 CDN 库,那么你需要禁用默认的,这样你就不会让 jQuery 加载两次。

Codex: Function Reference/wp enqueue script

Codex: Child Themes

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

【讨论】:

  • Enqueue CSS 代码是否应该改用get_stylesheet_directory_uri()?您的示例让我使用父主题的样式表,不是吗?否则,我放入子主题 style.css 中的代码如何生效?
  • ggkid2002 - 你是对的。为您的子主题使用 get_stylesheet_directory_uri()(甚至为您的子主题 style.css 使用 get_stylesheet_uri())。此外,无需将其分成两个函数并且不要将 jquery 作为自身的依赖项包含在内(请参阅答案的 jquery enqueue)。
  • @Joe-NewNine 除了依赖部分,我什么都理解。他的代码向您表明他使用 jquery 作为其自身的依赖项怎么样?
  • wp_enqueue_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js', array('jquery') '2.1.3 ', 真的 );
  • 不,已更改。您是否检查过它是否默认加载?
最近更新 更多