body_class( string|string[] $class = '' )
Displays the class names for the body element.
Contents
Description Description
Parameters Parameters
- $class
-
(string|string[]) (Optional) Space-separated string or array of class names to add to the class list.
Default value: ''
Source Source
File: wp-includes/post-template.php
function body_class( $class = '' ) {
// Separates class names with a single space, collates class names for body element
echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
}
Expand full source code Collapse full source code View on Trac
Changelog Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
More Information More Information
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):
<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 */
}
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
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:
The results would be:
Add New Classes via Filter
You can add additional body classes by filtering the {@see ‘body_class’} hook.
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', function( $classes ) { return array_merge( $classes, array( 'class-name' ) ); } );Here’s a solution for adding a body class to a specific page template:
add_filter( 'body_class', 'custom_class' ); function custom_class( $classes ) { if ( is_page_template( 'page-example.php' ) ) { $classes[] = 'example'; } return $classes; }The result on the front-end:
The above example about how to remove inline classes via filters is incorrect.
Here is the correct way to do it:
add_filter('body_class', function (array $classes) { if (in_array('class-to-remove', $classes)) { unset( $classes[array_search('class-to-remove', $classes)] ); } return $classes; });# Function
body_class()add some STATIC classes depends on the page, post, archive, blog, search, 404 etc.# List of all default static classes which are added to
.rtl { /* # Checks if current locale is RTL (Right To Left script). */ } .home { /* # Depends on the site’s “Front page displays” Reading Settings ‘show_on_front’ and ‘page_on_front’. \n If you set a static page for the front page of your site, this function will return true when viewing that page. */ } .blog { /* # Add if blog view homepage, otherwise false. */ } .archive { /* For # Month, Year, Category, Author, Post Type archive */ } .date { /* # For date archive */ } .search { /* # For search */ } .search-results { /* # If found posts in search result */ } .search-no-results { /* # If NOT found any posts in search result */ } .paged { /* # On paged result and not for the first page */ } .attachment { /* # On attachment page */ } .error404 { /* # On 404 page */ } .single { /* # Add for any post type, except {attachments} and {pages} */ } .single-format-standard { /* # standard post format */ } .post-type-archive { /* # post type archive page */ } .author { /* # author page */ } .category { /* # category page */ } .tag { /* # Tags page */ } .page { /* # existing single page */ } .page-parent { /* # Parent page only */ } .page-child { /* # Child page only */ } .page-template { /* # Page templates only */ } .page-template-default { /* # Default page templates only */ } .logged-in { /* # Logged in user */ } .admin-bar { /* # Only in admin bar */ } .no-customize-support { /* # Only in admin bar */ } .custom-background { /* # If theme support 'custom-background' or get_background_image() */ } .wp-custom-logo { /* # If the site has a custom logo. */ }Expand full source codeCollapse full source code
# Function
body_class()also add some DYNAMIC classes as below:Expand full source codeCollapse full source code
You can check all these in function get_body_class()
To remove a class of the body_class function you have to do that:
add_filter( 'body_class', function( $classes ) { foreach($classes as $key => $class) { if( $class == "class-to-remove" ){ unset($classes[$key]); } } return $classes; }, 1000);The other functions mentioned above are not working since they are ignoring the keys of the array $classes and do not have the needed priority.
function wp_body_classes( $classes ) { $classes[] = 'class-name'; return $classes; } add_filter( 'body_class','wp_body_classes' );This is from
get_page_template_slugas used by this function and is important to remember:If the template is stored in a Theme’s subdirectory (or a Parent Theme’s subdirectory of a Child Theme), the value of the wp_postmeta is both the folder and file names, e.g.:
my-templates/my-custom-template.phpAdding maijabrazen. Example if want remove author id from body class. If wish to remove Author name, use user_nicename
add_filter('body_class', function (array $classes) { $author = 'author-'.get_the_author_meta('ID') ; if (in_array($author, $classes)) { unset( $classes[array_search($author, $classes)] ); } return $classes; });Remove Classes via Filters
Remove an existing body class by un-setting the key from the
$classesarray.// Removes a class from the body_class array. add_filter( 'body_class', function( $classes ) { if ( isset( $classes['class-to-remove'] ) ) { unset( $classes['class-to-remove'] ); } return $classes; } );