1. Home
  2. Docs
  3. WordPress Basic
  4. body 클래스에 카테고리 슬러그 추가

body 클래스에 카테고리 슬러그 추가

싱글 글 보기에서 특정 카테고리별로 화면 요소를 제어하고 싶을 때, 최상위 body 태그에 카테고리 슬러그 이름의 클래스를 추가하여 이용할 수 있다.

add_filter('body_class','custom_add_category_class_to_single_body');
function custom_add_category_class_to_single_body($classes) {
	if (is_single() ) {
	  global $post;
	  foreach((get_the_category($post->ID)) as $category) {
		// add category slug to the $classes array
		$classes[] = $category->category_nicename;
	  }
	}
	// return the $classes array
	return $classes;
}