Title: category class in single
Last modified: July 10, 2019

---

# category class in single

 *  Resolved [inula](https://wordpress.org/support/users/janetb/)
 * (@janetb)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/category-class-in-single/)
 * I would like to put category class in single post. I searched the forum and found
   this:
 *     ```
       add_filter('body_class','add_category_to_single');
       function add_category_to_single($classes, $class) {
       	if (is_single() ) {
       		global $post;
       		foreach((get_the_category($post->ID)) as $category) {
       			echo $category->cat_name . ' ';
       			// add category slug to the $classes array
       			$classes[] = 'category-'.$category->slug;
       		}
       	}
       	// return the $classes array
       	return $classes;
       }
       ```
   
 * When I put this in functions.php I get error:
    ‘ArgumentCountError thrown Too
   few arguments to function add_category_to_single(), 1 passed in /home/vcsleen/
   domains/vcsleen.nl/public_html/wp-includes/class-wp-hook.php on line 288 and 
   exactly 2 expected’
 * What am I doing wrong???
 * In same functions.php I use this:
 *     ```
       // category id in body and post class
       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');
       ```
   
 * Maybe that causes the error?
    -  This topic was modified 7 years, 1 month ago by [inula](https://wordpress.org/support/users/janetb/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/category-class-in-single/#post-11720128)
 * instead of `function add_category_to_single($classes, $class) {`
    use `function
   add_category_to_single($classes) {`
 * also, what is the purpose if this line (maybe other than testing):
    `echo $category-
   >cat_name . ' ';` it is not needed to generate the additional CSS classes – consider
   to remove it
 *  Theme Author [Tom](https://wordpress.org/support/users/edge22/)
 * (@edge22)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/category-class-in-single/#post-11720226)
 * Thanks, Michael! This should be the final function:
 *     ```
       add_filter( 'body_class', function( $classes ) {
           if ( is_single() ) {
               global $post;
   
               foreach( ( get_the_category( $post->ID ) ) as $category ) {
                   $classes[] = 'category-' . $category->slug;
               }
           }
   
           return $classes;
       } );
       ```
   
 *  Thread Starter [inula](https://wordpress.org/support/users/janetb/)
 * (@janetb)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/category-class-in-single/#post-11721283)
 * Thanks again, Tom!
 *  Theme Author [Tom](https://wordpress.org/support/users/edge22/)
 * (@edge22)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/category-class-in-single/#post-11723148)
 * No problem 🙂

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘category class in single’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/generatepress/3.6.1/screenshot.
   png)
 * GeneratePress
 * [Support Threads](https://wordpress.org/support/theme/generatepress/)
 * [Active Topics](https://wordpress.org/support/theme/generatepress/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/generatepress/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/generatepress/reviews/)

 * 4 replies
 * 3 participants
 * Last reply from: [Tom](https://wordpress.org/support/users/edge22/)
 * Last activity: [7 years, 1 month ago](https://wordpress.org/support/topic/category-class-in-single/#post-11723148)
 * Status: resolved