Add Page Slug To Body Class in WordPress

This will add the page slug to the body class when on a page.

/**
 * add page slug to body class, if on a page
 */

add_filter('body_class','smartestb_pages_bodyclass');
function smartestb_pages_bodyclass($classes) {
	if (is_page()) {
		// get page slug
		global $post;
		$slug = get_post( $post )->post_name;

		// add slug to $classes array
		$classes[] = $slug;
		// return the $classes array
		return $classes;
	} else { 
		return $classes;
	}
}

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]