How to Add Page Body Class to your WordPress Themes

How to Add Page Body Class to your WordPress Themes

The body class in WordPress is a class or series of classes that are applied to the HTML body element. This is useful for applying unique styles to different areas of a WordPress site as body classes can be added conditionally.

How to Add to the Body Class?

There are a few methods available to us for adding new body classes within WordPress.

Editing the Theme: Passing a Body Class Value

The body class is normally included in a theme using the following code:

<body <?php body_class(); ?>>

If would like to add your own class to this, you can pass an argument in to the function, like so:

<body <?php body_class( 'custom-class' ); ?>>

This would add a body class of custom-class on each page of your WordPress site.

How to Add Multiple Body Classes in Theme?

There may be times when you want to add more than one body class that can we get by using a simple array:

<body <?php body_class( array( "class1", "class2", "class3" ) ); ?>>

This takes all of the classes in the array, and passes them to the body_class function.

Hope you will find this tutorial helpful.