易多云免备案虚拟主机,新用户免费试用15天,一键安装Wordpress, 点此查看详情!
本文目录
[隐藏]
- 1加载外部 jQuery 库和主题自定义的脚本、样式
- 2加载WP默认 jQuery 库和主题自定义的脚本、样式
- 3加载 print.css 到你的WordPress主题
- 4使用 wp_enqueue_scripts 替换 init
- 5使用 is_single() 只在文章加载脚本或CSS
- 6使用 is_page() 只在页面加载脚本或CSS
- 7使用 admin_enqueue_scripts 加载脚本到后台
- 8加载脚本和CSS到WordPress登录界面
- 9加载脚本和CSS到WordPress插件
- 10从插件加载脚本和CSS到后台管理区
- 11从插件加载脚本和CSS到插件设置页面
- 12将 jQuery 库移动到页脚
- 13根据不用的用户角色和功能加载jQuery和CSS
- 14为可以“编辑文章”的管理员加载脚本和CSS
- 15为所有登录用户加载脚本和CSS
- 16为管理员以外的已登录用户加载脚本和CSS
- 17最后的提示
正确加载 jQuery、Javascript 和 CSS 到你的WordPress网站也许是一件比较痛苦的事情。 本文将讲解如何使用WordPress官方推荐的方式来加载脚本/ CSS。
有两种常用的 add_action 钩子可以加载 脚本和CSS到WordPress:
- init: 确保始终为您的网站头部加载脚本和CSS(如果使用home.php,index.php或一个模板文件),以及其他“前端”文章、页面和模板样式。
- wp_enqueue_scripts:“适当”的钩子方法,并不总是有效的,根据你的WordPress设置。
下面的所有例子都在WordPress多站点模式、WordPress 3.4.2 通过测试(如果不支持后续版本,请留言告知)
加载外部 jQuery 库和主题自定义的脚本、样式
下面这个例子在 add_action 钩子中使用 init。使用 init 有两个原因,一是因为我们正在注销WordPress默认的jQuery库,然后加载谷歌的jQuery库;二是确保在WordPress的头部就加载脚本和CSS。
使用if ( !is_admin() )是为了确保这些脚本和css只在前端加载,不会再后台管理界面加载。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** Google jQuery Library, Custom jQuery and CSS Files */
function myScripts() {
wp_register_script( 'google', 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js' );
wp_register_script( 'default', get_template_directory_uri() . '/jquery.js' );
wp_register_style( 'default', get_template_directory_uri() . '/style.css' );
if ( !is_admin() ) { /** Load Scripts and Style on Website Only */
wp_deregister_script( 'jquery' );
wp_enqueue_script( 'google' );
wp_enqueue_script( 'default' );
wp_enqueue_style( 'default' );
}
}
add_action( 'init', 'myScripts' ); |
加载WP默认 jQuery 库和主题自定义的脚本、样式
第3行:使用 array('jquery') 是为了告诉 WordPress 这个 jquery.js 是依赖WordPress 的jQuery库文件,从而使 jquery.js 在WordPress jQuery库文件后加载。
1
2
3
4
5
6
7
8
9
10
|
/** Add Custom jQuery and CSS files to a Theme */
function myScripts() {
wp_register_script( 'default', get_template_directory_uri() . '/jquery.js', array('jquery'), '' );
wp_register_style( 'default', get_template_directory_uri() . '/style.css' );
if ( !is_admin() ) { /** Load Scripts and Style on Website Only */
wp_enqueue_script( 'default' );
wp_enqueue_style( 'default' );
}
}
add_action( 'init', 'myScripts' ); |
加载 print.css 到你的WordPress主题
第 3 行:最后的 ‘print’是媒体屏幕调用,确保 print.css 在网站的打印机中的文件加载时才加载。
1
2
3
4
5
6
7
8
|
/** Adding a Print Stylesheet to a Theme */
function myPrintCss() {
wp_register_style( 'print', get_template_directory_uri() . '/print.css', '', '', 'print' );
if ( !is_admin() ) { /** Load Scripts and Style on Website Only */
wp_enqueue_style( 'print' );
}
}
add_action( 'init', 'myPrintCss' ); |
使用 wp_enqueue_scripts 替换 init
如果你要在文章或页面加载唯一的脚本,那就应该使用 wp_enqueue_scripts 替换 init。使用 wp_enqueue_scripts 仅仅只会在前台加载脚本和CSS,不会在后台管理界面加载,所以没必要使用 !is_admin() 判断。
使用 is_single() 只在文章加载脚本或CSS
第 3 行的 # 替换为文章的ID就可以让脚本和css只加载到那篇文章。当然,如果直接使用 is_single() (不填ID),就会在所有文章加载脚本和CSS。
1
2
3
4
5
6
7
|
/** Adding Scripts To A Unique Post */
function myScripts() {
if ( is_single(#) ) { /** Load Scripts and Style on Posts Only */ /** Add jQuery and/or CSS Enqueue */
}
}
add_action( 'wp_enqueue_scripts', 'myScripts' ); |
使用 is_page() 只在页面加载脚本或CSS
第 3 行的 # 替换为页面的ID就可以让脚本和css只加载到那个页面。当然,如果直接使用 is_single() (不填ID),就会在所有页面加载脚本和CSS。
1
2
3
4
5
6
7
|
/** Adding Scripts To A Unique Page */
function myScripts() {
if ( is_page(#) ) { /** Load Scripts and Style on Pages Only */ /** Add jQuery and/or CSS Enqueue */
}
}
add_action( 'wp_enqueue_scripts', 'myScripts' ); |
使用 admin_enqueue_scripts 加载脚本到后台
这个例子将在整个后台管理界面加载脚本和CSS。这个方法不推荐用在插件上,除非插件重建了整个后台管理区。
第 10 行使用 admin_enqueue_scripts 替换了 init 或 wp_enqueue_scripts
第 5、6 行,如果你要自定义后台管理区,你可以需要禁用默认的WordPress CSS调用。
1
2
3
4
5
6
7
8
9
10
|
/** Adding Scripts To The WordPress Admin Area Only */
function myAdminScripts() {
wp_register_script( 'default', get_template_directory_uri() . '/jquery.js', array('jquery'), '' );
wp_enqueue_script( 'default' );
//wp_deregister_style( 'ie' ); /** removes ie stylesheet */
//wp_deregister_style( 'colors' ); /** disables default css */
wp_register_style( 'default', get_template_directory_uri() . '/style.css', array(), '', 'all' );
wp_enqueue_style( 'default' );
}
add_action( 'admin_enqueue_scripts', 'myAdminScripts' ); |
加载脚本和CSS到WordPress登录界面
第 6 行:我无法弄清楚如何在在登录页面注册/排序 CSS文件,所以这行手动添加样式表。
第 10-14行:用来移除WordPress默认的样式表。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/** Adding Scripts To The WordPress Login Page */
function myLoginScripts() {
wp_register_script( 'default', get_template_directory_uri() . '/jquery.js', array('jquery'), '' );
wp_enqueue_script( 'default' );
?>
<link rel='stylesheet' id='default-css' href='<?php echo get_template_directory_uri() . '/style.css';?>' type='text/css' media='all' />
<?php }
add_action( 'login_enqueue_scripts', 'myLoginScripts' );
/** Deregister the login css files */
function removeScripts() {
wp_deregister_style( 'wp-admin' );
wp_deregister_style( 'colors-fresh' );
}
add_action( 'login_init', 'removeScripts' ); |
加载脚本和CSS到WordPress插件
WordPress插件加载脚本和CSS也是常见的。主要的不同之处在于文件的 URL。主题使用的是 get_template_directory_uri ,而插件应该用 plugins_url ,因为文件是从插件目录进行加载的。
从插件加载脚本和CSS
这个例子将在整个网站前端加载脚本和CSS。
1
2
3
4
5
6
7
8
9
10
|
/** Global Plugin Scripts for Outside of Website */
function pluginScripts() {
wp_register_script( 'plugin', plugins_url( 'jquery.js' , __FILE__ ), array('jquery'), '' );
wp_register_style( 'plugin', plugins_url( 'style.css' , __FILE__ ) );
if ( !is_admin() ) { /** Load Scripts and Style on Website Only */
wp_enqueue_script( 'plugin' );
wp_enqueue_style( 'plugin' );
}
}
add_action( 'init', 'pluginScripts' ); |
从插件加载脚本和CSS到后台管理区
如果你需要在整个后台管理区加载脚本和CSS,就使用 admin_enqueue_scripts 替换 init。
1
2
3
4
5
6
7
8
|
/** Global Plugin Scripts for The WordPress Admin Area */
function pluginScripts() {
wp_register_script( 'plugin', plugins_url( 'jquery1.js' , __FILE__ ), array('jquery'), '' );
wp_enqueue_script( 'plugin' );
wp_register_style( 'plugin', plugins_url( 'style1.css' , __FILE__ ) );
wp_enqueue_style( 'plugin' );
}
add_action( 'admin_enqueue_scripts', 'pluginScripts' ); |
从插件加载脚本和CSS到插件设置页面
例子只会加载所需的脚本和CSS到插件设置页面,不会在管理区的其他页面加载。
第 3 行:自定义 page= 后面的值为你的插件设置页面
1
2
3
4
5
6
7
8
9
10
|
/** Adding Scripts On A Plugins Settings Page */
function pluginScripts() {
if ( $_GET['page'] == "plugin_page_name.php" ) {
wp_register_script( 'plugin', plugins_url( 'jquery.js' , __FILE__ ), array('jquery'), '' );
wp_enqueue_script( 'plugin' );
wp_register_style( 'plugin', plugins_url( 'style.css' , __FILE__ ) );
wp_enqueue_style( 'plugin' );
}
}
add_action( 'admin_enqueue_scripts', 'pluginScripts' ); |
将 jQuery 库移动到页脚
你不能将WordPress默认的jQuery 库移动到页面底部,但是你可以将自定义的jQuery 或其他外部jQuery 库(比如Google的)移动到底部。不要将CSS移动到页面底部。
第 3、4 行:最后的 ‘true’告诉WordPress在页面底部加载这些脚本。
1
2
3
4
5
6
7
8
9
10
11
|
/** Moves jQuery to Footer */
function footerScript() {
wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.js"), false, '', true );
wp_register_script( 'default', get_template_directory_uri() . '/jquery.js', false, '', true );
if ( !is_admin() ) { /** Load Scripts and Style on Website Only */
wp_deregister_script( 'jquery' );
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'default' );
}
}
add_action( 'init', 'footerScript' ); |
根据不用的用户角色和功能加载jQuery和CSS
如果你的网站有作者、编辑和其他管理员,你可能需要通过 jQuery 来为他们显示不同的信息。你需要使用 current_user_can 确定登录的用户的 角色和功能 。
下面三个例子中,如果用户已经登录,将在整个网站加载这些脚本和CSS。使用 !is_admin() 包装 enqueue_script 确保只在前台加载,或者在 add_action 使用 admin_enqueue_scripts 就可以确保只在后台管理区加载。
为可以“编辑文章”的管理员加载脚本和CSS
只对超级管理员和网站管理员生效
1
2
3
4
5
6
7
|
/** Add CSS & jQuery based on Roles and Capabilities */
function myScripts() {
if ( current_user_can('edit_posts') ) {
/** Add jQuery and/or CSS Enqueue */
}
}
add_action( 'init', 'myScripts' ); |
为所有登录用户加载脚本和CSS
1
2
3
4
5
6
7
|
/** Admins / Authors / Contributors / Subscribers */
function myScripts() {
if ( current_user_can('read') ) {
/** Add jQuery and/or CSS Enqueue */
}
}
add_action( 'init', 'myScripts' ); |
为管理员以外的已登录用户加载脚本和CSS
1
2
3
4
5
6
7
|
/** Disable for Super Admins / Admins enable for Authors / Contributors / Subscribers */
function myScripts() {
if ( current_user_can('read') && !current_user_can('edit_users') ) {
/** Add jQuery and/or CSS Enqueue */
}
}
add_action( 'init', 'myScripts' ); |
最后的提示
上面的例子如果使用相同的add_action,就可以被合并成一个单一的函数。 换句话说,您可以使用多个 if 语句在一个函数中分裂了你的脚本和CSS调用,如:if_admin!if_admin,is_page,is_single和current_user_can的,因为每次使用相同的add_action的init。
- 原文:http://technerdia.com/1789_include-jquery-css.html
- 编译:倡萌@WordPress大学
请尊重我们的辛苦付出,未经允许,请不要转载 WordPress大学 的文章!
上一篇:如何获取 WordPress 各类页面的链接
下一篇:为你的 WordPress 主题创建内置的联系表单
貌似在这个方法里没有判断是否是在首页加载的,我看了一下is_home和is_front_page都是$wp_query对象中的值,如果我要按照你的方法来做,在init这个hook中添加判断是否是首页才加载css或者js该如何操作,还是只能另辟蹊径?求大神回答
照样使用 is_home 或 is_front_page 判断就可以了
比如
if(is_home() || is_front_page()) {
//你的代码
}
不好意思,我的问题没有表达清楚,打个比方,我在init这个hook中判断is_front_page和is_home是没有效果的,因为在init中$wp-query这个对象还没被创建,而is_front_page和is_home两个函数内部实现都是调用$wp_query的函数。我是这个意思,但你的博客里写的是在init这个hook中添加css和js才是正确方式,那我该如何解决这个问题?
function shaojun_init() {
/* css */
wp_register_style( 'front', get_template_directory_uri() . '/style/front/front.css');
if(is_front_page() || is_home()) {
wp_enqueue_style( 'front' );
}
}
add_action( 'init', 'shaojun_init' );
把
add_action( 'init', 'shaojun_init' );
改成add_action( 'wp_enqueue_scripts', 'shaojun_init' );
试试如果把加载的JS和CSS都改为绝对链接好吗?比如一些主题支持在后台自定义CSS样式,那样会不会导致自定义失效?
谢谢您的的分享,真的很受用,很详细。
虽然纠结了一下文件链接位置的问题,后来查了一下百度发现是plugins_url这个函数的问题,将这个函数整个去掉就可以了,希望可以给有帮助的朋友看到
最后还有一个小问题,我在修改文件地址时候,发现不能把文件地址放到/wp-admin/这个文件夹下,好像是wordpress的问题,不知道大神有什么方法可以把我自己的css文件放到这个文件夹下
/wp-admin/是WP的核心目录,先不说是不是WordPress本身的限制问题,我都不推荐你将自己的文件放到这个目录,因为只要更新WP的版本,/wp-admin/目录的所有文件都会被删除,然后替换为新版本的文件,如果你将自己的文件放在这个目录,一更新WP,就没了。只有/wp-content/这个目录下的文件是在更新时不会被删除和替换的,所以你自己的文件应该放在这个目录下
因为当时本意是想自己修改wordpress后台的样子,我觉得他现在的样子真的很难看,所以很自然的想把自己的css文件放置在wp-admin目录下,现在看到您说的,真的发现自己很白痴,虽然现在我也已经暂时放弃修改后台样子了,不过还是谢谢您的回复,谢谢您对于我这种菜鸟的关注O(∩_∩)O~
能否给个加载并引用实例:加载 bootstrap到主题?
文章的各种引用方法和示例都给了,还想怎样呢
比如我已经加载了bootstrap到functions了,添加一个.nav-stacked类到菜单nav-menu,具体是怎么写,求demo!!!
抱歉,没时间帮你折腾这个
哦!慢慢折腾吧,能加你扣扣吗?