25% off ProSNIPC25OFF

Add Slug and Category Body Classes

Adds the current page slug and post category slugs as extra CSS body classes for precise per-page styling.

PHPby SnipCraft
php
<?php
add_filter( 'body_class', 'scseed_slug_and_category_body_classes' );
if ( ! function_exists( 'scseed_slug_and_category_body_classes' ) ) {
    function scseed_slug_and_category_body_classes( $classes ) {
        global $post;
        if ( ( is_single() || is_page() ) && ! empty( $post->post_name ) ) {
            $classes[] = 'slug-' . sanitize_html_class( $post->post_name );
        }
        if ( is_single() ) {
            $categories = get_the_category( $post->ID );
            foreach ( $categories as $cat ) {
                $classes[] = 'cat-slug-' . sanitize_html_class( $cat->slug );
            }
        }
        return $classes;
    }
}
#body-class#css#display#frontend#theming