Codex tools: Log in
Languages: English • 日本語 • (Add your language)
Contents |
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
The body_class CSS classes appear based upon the pageview Conditional Tags as follows.
Site front page: home
Blog posts index: blog
Single post pages: single postid-{ID}
Archive index pages: archive
Static page: page page-id-{ID}
Search results page: search
Multi-page index pages and paginated posts: paged. Multi-page index pages and paginated posts with more than 2 pages paged paged-n.
Error 404 page: error404
If the current user is logged in: logged-in
If text direction is set to "right-to-left": rtl
If using Custom Background to display the site background image or color the class selectors: custom-background
If the admin toolbar is displayed: admin-bar no-customize-support
The following example shows how to implement the body_class template tag into a theme.
<body <?php body_class(); ?>>
The actual HTML output might resemble something like this (the About the Tests page from the Theme Unit Test):
<body class="page page-id-2 page-parent page-template-default logged-in">
In the WordPress Theme stylesheet, add the appropriate styles, such as:
.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 */
}
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:
<body <?php body_class('class-name'); ?>>
The results would be:
<body class="page page-id-2 page-parent page-template-default logged-in class-name">
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:
// 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.
// 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');
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
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();
}
}
body_class() is located in wp-includes/post-template.php.
body_class(), next_image_link(), next_post_link(), next_posts_link(), post_class(), post_password_required(), posts_nav_link(), previous_image_link(), previous_post_link(), previous_posts_link(), single_post_title, sticky_class(), the_category(), the_category_rss(), the_content(), the_content_rss(), the_excerpt(), the_excerpt_rss(), the_ID(), the_meta(), the_shortlink(), the_tags(), the_title(), the_title_attribute(), the_title_rss(), wp_link_pages()