Displays the class names for the body element.
$css_classstring|string[]optional
Default:''
This function gives the body element different classes and can be added, typically, in the header.php’s HTML body tag.
Basic Usage
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):
[html]
<body class="page page-id-2 page-parent page-template-default logged-in">
[/html]
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 */
} function body_class( $css_class = '' ) {
// Separates class names with a single space, collates class names for body element.
echo 'class="' . esc_attr( implode( ' ', get_body_class( $css_class ) ) ) . '"';
}
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/body_class