WordPress body_class() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Prints a class attribute

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().

WordPress
<body <?php body_class(); ?>>
CallWhat 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
WordPress
body_class( string|string[] $css_class = '' ): void

Parameters

ParameterDescription
$css_classstring|string[]. One or more extra classes to add. Default is empty.

Return Value

Return ValueDescription
voidNothing 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.

WordPress
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
  <?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>

How It Works

  1. body_class() prints class="..." with classes that match the current page.
  2. On the blog home page WordPress adds home and blog.
  3. 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.

WordPress
<body <?php body_class( 'dark-mode' ); ?>>

How It Works

  1. The default classes are made first.
  2. Your class dark-mode is added at the end.
  3. 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.

WordPress
<?php
function mytheme_body_classes( $classes ) {
  if ( is_single() ) {
    $classes[] = 'reading-mode';
  }
  return $classes;
}
add_filter( 'body_class', 'mytheme_body_classes' );

How It Works

  1. The filter gets the array of classes and returns it.
  2. is_single() is true on a single post, so the class is added there only.
  3. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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