Add custom body classes to WordPress

If you want to add a custom class to your WordPress page based on custom condition i.e. the current page isn’t a front page.

Here is an example on how to use body_class filter.

/**
 * Add custom body classes.
 *
 * @param  array $classes current array of classes.
 * @return array
 */
function text_domain_body_classes( $classes ) {
	// if is page && not front page.
	if ( is_page() && ! is_front_page() ) {
		$classes[] = 'internal-page';
	}
	return $classes;
}
add_filter( 'body_class', 'text_domain_body_classes', 20, 1 );

If you find the above code is useful please share, thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top