Add category id to body and post class

This snippet is very handy if you wish to add category ID to body and post class for ex. targeting CSS styles and such.

Instructions
Add this to your functions.php file.

function category_id_class($classes) {
     global $post;
          foreach((get_the_category($post->ID)) as $category)
               $classes [] = 'cat-' . $category->cat_ID . '-id';
               
     return $classes;
}

add_filter('post_class', 'category_id_class');
add_filter('body_class', 'category_id_class');

Go to wp-snippet.com [source]