The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Add the Page Slug to Body Class

Shared by Ankit Panchal · April 30, 2026

1 copy
43 views
4 upvotes
Back to Snippets

Add the page slug to the body class for better styling.

function wpcode_snippet_add_slug_body_class( $classes ) {
	global $post;
	if ( isset( $post ) ) {
		$classes[] = $post->post_type . '-' . $post->post_name;
	}

	return $classes;
}

add_filter( 'body_class', 'wpcode_snippet_add_slug_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