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:
    0
  2. Clicks this month:
    0
  3. Overall rating:
    5.00/5

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

Comments

avatarnicknone almost 2 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.

Your Comment

You must be logged in to post a comment.