WordPress body_class() Function

Definition
In WordPress, body_class() prints a class attribute full of helpful CSS classes. You put it on the <body> tag in header.php.
The classes describe the page, such as home, single, page, archive, or logged-in. Use them in your CSS to style one kind of page differently.
Syntax
A few ways to call body_class().
<body <?php body_class(); ?>>
| Call | What it does |
|---|
body_class() | Print the default classes. |
body_class( 'dark-mode' ) | Add your own class. |
body_class( array( 'wide', 'no-sidebar' ) ) | Add several classes. |
get_body_class() | Return the classes as an array instead of printing. |
It prints the whole class="..." attribute, so do not write class= yourself on the same tag.
Full declaration
body_class( string|string[] $css_class = '' ): void
Parameters
| Parameter | Description |
|---|
$css_class | string|string[]. One or more extra classes to add. Default is empty. |
Return Value
| Return Value | Description |
|---|
| void | Nothing is returned. The class attribute is printed. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Add it to the body tag
Open header.php and add body_class() to the body tag. Here is the result on the blog home page.
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
How It Works
body_class() prints class="..." with classes that match the current page.- On the blog home page WordPress adds
home and blog. - Real sites often show more classes, such as
logged-in.
Example 2 — Add your own class
Pass a string to add a class of your own after the default ones.
<body <?php body_class( 'dark-mode' ); ?>>
<body class="post-template-default single single-post postid-12 single-format-standard dark-mode">
How It Works
- The default classes are made first.
- Your class
dark-mode is added at the end. - In CSS you can now write
.dark-mode { ... }.
Example 3 — Add a class only on single posts
Use the body_class filter to add a class by rule.
<?php
function mytheme_body_classes( $classes ) {
if ( is_single() ) {
$classes[] = 'reading-mode';
}
return $classes;
}
add_filter( 'body_class', 'mytheme_body_classes' );
<body class="post-template-default single single-post postid-12 single-format-standard reading-mode">
How It Works
- The filter gets the array of classes and returns it.
is_single() is true on a single post, so the class is added there only.- On other pages the extra class is not added.
Frequently Asked Questions
It prints a class attribute with CSS classes that describe the current page, such as home, single, page, or archive.
Put it inside the opening body tag in header.php, like this: <body <?php body_class(); ?>>.
Pass it as the first argument, for example body_class( 'dark-mode' ), or use the body_class filter.
body_class() belongs on the body tag and describes the whole page. post_class() belongs on each post wrapper and describes one post.
Yes. Use get_body_class(). It returns an array of classes.
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful