如何将 Bootstrap CDN 添加到我的 Wordpress [英] How to add Bootstrap CDN to my Wordpress
问题描述
我想为我的 Wordpress 使用 Bootstrap 框架.如何在 functions.php 中进行编辑?我找到某个地方告诉这样的代码
I want to use Bootstrap framework for my Wordpress.. how to edit in the functions.php ? i find somewhere tell the code like this
function enqueue_my_scripts() {
wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js', array('jquery'), '1.9.1', true); // we need the jquery library for bootsrap js to function
wp_enqueue_script( 'bootstrap-js', '//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js', array('jquery'), true); // all the bootstrap javascript goodness
}
add_action('wp_enqueue_scripts', 'enqueue_my_scripts');
最后一段中的第一段 function enqueue_my_scripts()
和 add_action('wp_enqueue_scripts', 'enqueue_my_scripts');
是什么意思?
and what is that mean of first paragraph function enqueue_my_scripts()
and add_action('wp_enqueue_scripts', 'enqueue_my_scripts');
in the last paragraph ?
推荐答案
这些被称为钩子",你可以在这里阅读它们:http://codex.wordpress.org/Plugin_API
Those are called "hooks" and you can read about them here: http://codex.wordpress.org/Plugin_API
否则你的代码基本上是正确的,只有一个错误.你有 jQuery 作为对 jQuery 的依赖,这意味着它永远不会加载,随后引导程序永远不会加载:
Otherwise your code is essentially correct, with one mistake. You have jQuery as a dependency to jQuery, which means it is never loaded and subsequently bootstrap is never loaded:
wp_enqueue_script(
'jquery',
'//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
array( 'jquery' ), // <-- Problem
'1.9.1',
true
);
解决方案:
wp_enqueue_script(
'jquery',
'//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
array(), // <-- There we go
'1.9.1',
true
);
但是,还有一件事.WordPress 已经准备好 jQuery 供您入队(即 wp_enqueue_script( 'jquery' );
将加载 jQuery 的本地副本).这不是必需的,但我认为最好的做法是将带有后缀的脚本的 CDN 版本加入队列,即:
But, there's one more thing. WordPress already has jQuery ready for you to enqueue (i.e. wp_enqueue_script( 'jquery' );
will load the local copy of jQuery). It isn't necessary but I think it is best practice to enqueue a CDN version of a script with a suffix, i.e.:
wp_enqueue_script(
'jquery-cdn',
'//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js',
array(),
'1.9.1',
true
);
// And then update your dependency on your bootstrap script
// to use the CDN jQuery:
wp_enqueue_script(
'bootstrap-js',
'//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js',
array( 'jquery-cdn' ),
true
);
这篇关于如何将 Bootstrap CDN 添加到我的 Wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!