I always used the following code to add a class to the body element of posts.

//Page Slug Body Class
function add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
return $classes;
add_filter( 'body_class', 'add_slug_body_class' );

Now I'm seeing this doesn't work and it just throws the body tag, ignoring the class. I didn't change the code, it simply stopped working. I'm using WP version 4.6.1.

Did it stop working or it's just that there's a problem (maybe a plugin or something in my code)?

  • It seems to be working for me (tested on single template). What template are you working with? – Dave Romsey Sep 23 '16 at 22:24
  • tested on page.php and single.php . well, guess there's something else then – Rick Alvarez Sep 23 '16 at 22:31
  • 2
    A possible cause is that your theme 's header.php is not adding the body_class function to the body starting tag. you can confirm if this is the case quickly by editing template's header or where they located <body> tag – Ahmed Fouad Sep 23 '16 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.