Filters the list of CSS body class names for the current post or page.
Parameters
$classes
string[]- An array of body class names.
$css_class
string[]- An array of additional class names added to the body.
Source
$classes = apply_filters( 'body_class', $classes, $css_class );
Changelog
Version | Description |
---|---|
2.8.0 | Introduced. |
Add New Classes
You can add additional body classes by filtering the ‘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:
Following function adds CSS classes that may be useful
Remove Classes
Remove an existing body class by un-setting the key from the
$classes
array.The body_class filter allows you to modify the array of classes applied to the tag. This can be useful for styling pages differently based on their context.
Open functions.php: Go to your theme directory and open functions.php. This file is where you add custom functions and hooks.
Understanding the Conditions
1. is_home() : Adds the home-page class if the current page is the home page.
2. is_single() : Adds the single-post class if the current page is a single post.
3. is_page_template() : Adds the custom-template class if the page is using a specific template.
4. is_user_logged_in() : Adds the logged-in class if the user is logged in.
5. current_user_can() : Adds the admin-user class if the current user has the ‘administrator’ role.