WordPress函数文档body_class()
根据不同的页面类型为body标签生成class 描述 Themes have a template tag f…
根据不同的页面类型为body标签生成class
描述
Themes have a template tag for the body tag which will help theme authors to style more effectively with CSS. The Template Tag is called body_class
. This function gives the body
element different classes and can be added, typically, in the header.php’s HTML body
tag.
用法
<body <?php body_class( $class ); ?>>
参数
How to pass parameters to tags with PHP function-style parameters
class
(string or array) (可选) One or more classes to add to the class attribute, separated by a single space.
默认值: null
示例
Implementation
The following example shows how to implement the body_class
template tag into a theme.
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<body <?php body_class(); ?>>
|
The actual HTML output might resemble something like this (the About the Tests page from the Theme Unit Test):
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<body class=“page page-id-2 page-parent page-template-default logged-in”>
|
In the WordPress Theme stylesheet, add the appropriate styles, such as:
1
2
3
4
5
6
7
8
9
10
11
12
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
.page {
/* styles for all posts within the page class */
}
.page–id–2 {
/* styles for only page ID number 2 */
}
.logged–in {
/* styles for all pageviews when the user is logged in */
}
|
Adding More Classes
By default, the only classes will be those described above.
To add more classes, the template tag’s parameter can be added. For example, to add a unique class to the same template used above:
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<body <?php body_class( ‘class-name’ ); ?>>
|
The results would be:
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<body class=“page page-id-2 page-parent page-template-default logged-in class-name”>
|
Add Classes By Filters
You can add additional body classes by filtering the body_class function.
To add the following to the WordPress Theme functions.php file, changing my_class_names and class-name to meet your needs:
1
2
3
4
5
6
7
8
9
10
11
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
// Add specific CSS class by filter
add_filter( ‘body_class’, ‘my_class_names’ );
function my_class_names( $classes ) {
// add ‘class-name’ to the $classes array
$classes[] = ‘class-name’;
// return the $classes array
return $classes;
}
|
To add a category class to single post pageviews and template files, add the following to the functions.php.
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
// add category nicenames in body and post class
function category_id_class( $classes ) {
global $post;
foreach ( get_the_category( $post->ID ) as $category ) {
$classes[] = $category->category_nicename;
}
return $classes;
}
add_filter( ‘post_class’, ‘category_id_class’ );
add_filter( ‘body_class’, ‘category_id_class’ );
|
Add Sidebar Classes
You can add additional body classes by filtering the body_class function, but what if you want to add a class only when the sidebar.php file is being shown? Here’s a working example you can post in your themes functions.php file to add a sidebar class to the output of body_class. From: Add CSS Class to body when Sidebar is Present
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
add_action( ‘wp_head’, create_function( ”, ‘ob_start();’ ) );
add_action( ‘get_sidebar’, ‘my_sidebar_class’ );
add_action( ‘wp_footer’, ‘my_sidebar_class_replace’ );
function my_sidebar_class( $name = ” ) {
static $class = ‘withsidebar’;
if ( ! empty( $name ) ) {
$class .= ‘ sidebar-‘ . $name;
}
my_sidebar_class_replace( $class );
}
function my_sidebar_class_replace( $c = ” ) {
static $class = ”;
if ( ! empty( $c ) ) {
$class = $c;
} else {
echo str_replace( ‘<body class=”‘, ‘<body class=”‘ . $class . ‘ ‘, ob_get_clean() );
ob_start();
}
}
|
相关
Theme Function(函数)s
- Function(函数): body_class()
- Function(函数): next_image_link()
- Function(函数): next_post_link()
- Function(函数): next_posts_link()
- Function(函数): post_class()
- Function(函数): post_password_required()
- Function(函数): posts_nav_link()
- Function(函数): previous_image_link()
- Function(函数): previous_post_link()
- Function(函数): previous_posts_link()
- Function(函数): single_post_title()
- Function(函数): sticky_class()
- Function(函数): the_category()
- Function(函数): the_category_rss()
- Function(函数): the_content()
- Function(函数): the_content_rss()
- Function(函数): the_excerpt()
- Function(函数): the_excerpt_rss()
- Function(函数): the_ID()
- Function(函数): the_meta()
- Function(函数): the_shortlink()
- Function(函数): the_tags()
- Function(函数): the_title()
- Function(函数): the_title_attribute()
- Function(函数): the_title_rss()
- Function(函数): wp_link_pages()
Hooks
- Filter Hook(过滤器钩子): ‘body_class’
- 原文:http://codex.wordpress.org/Function_Reference/body_class
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!