Blog

body_class() | Function | WordPress Developer Resources

Displays the class names for the body element.

$class

(string|string[]) (Optional) Space-separated string or array of class names to add to the class list.

Default value: ”

Top ↑

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.

>

The actual HTML output might resemble something like this (the About the Tests page from the Theme Unit Test):

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 */ }

Top ↑

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=”‘ . esc_attr( join( ‘ ‘, get_body_class( $class ) ) ) . ‘”‘; }

Expand full source code Collapse full source code View on Trac

Top ↑

Uses #Uses

Uses

Uses Description
wp-includes/formatting.php: esc_attr()

Escaping for HTML attributes.

wp-includes/post-template.php: get_body_class()

Retrieves an array of the class names for the body element.

Top ↑

Changelog

Version Description
2.8.0 Introduced.

Top ↑



Source