1. CSS
  2. Flash
  3. HTML
  4. Illustrator
  5. Java
  6. JavaScript
  7. Maya
  8. Photography
  9. Photoshop
  10. PHP
  11. Ruby
  12. Ruby on Rails
  13. 3ds Max

PHP: How to Add Page Slug in Body Class of your WordPress

  1. Clicks today:
  2. Clicks this month:
  3. Overall rating:

PHP » Templating — almost 6 years ago
Here's a handy WordPress code snippet which can be useful in adding post slug as class to the body tag.


avatarnicknone almost 6 years ago

we recommend that you put it in your theme’s functions.php file.

//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 you will start seeing a new body class being outputted like this: page-{slug}. Use that class to override your default styles and customize elements for specific pages.

For example, if you were trying to modify a div with widgets class. You can have your css like this:

sidebar .widgets{background: #fff; color: #000;}

.page-education #sidebar .widgets{background: #000; color: #fff;}

<a href="http://clapcreative.com/services/web-development-los-angeles/">Web Developer Los Angeles</a> Hope you will find this will be helpful.

avatarhayedia 4 months ago

I like this web blog very much, Its a real nice position to read and receive information. "The superior man is modest in his speech, but exceeds in his actions." by Confucius. More Info At: קידום אתרים

Your Comment

You must be logged in to post a comment.