Skip to main content
Login Join
Snippet · PHP

Add a Custom Body Class Based on Template

Shared by Hiral solanki · September 9, 2026 · @body_class

1 view
Back to Snippets
/**
 * Add a body class based on the active page template.
 *
 * @param array $classes Existing body classes.
 * @return array
 */
function mytheme_template_body_class( $classes ) {

	if ( is_page_template( 'templates/template-landing.php' ) ) {
		$classes[] = 'template-landing';
	}

	if ( is_page_template( 'templates/template-full-width.php' ) ) {
		$classes[] = 'template-full-width';
	}

	return $classes;
}

add_filter( 'body_class', 'mytheme_template_body_class' );
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments