get_body_class

Retrieve the classes for the body element as an array.

Description

(array) get_body_class( (string) $class = '' ); 

Returns (array)

Array of classes.

Parameters (1)

0. $class — Optional. (string) => ''
One or more classes to add to the class list.

Usage

  1. if ( !function_exists( 'get_body_class' ) ) { 
  2. require_once ABSPATH . WPINC . '/post-template.php'; 
  3.  
  4. // One or more classes to add to the class list. 
  5. $class = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_body_class($class); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/post-template.php  
  1. function get_body_class( $class = '' ) { 
  2. global $wp_query; 
  3.  
  4. $classes = array(); 
  5.  
  6. if ( is_rtl() ) 
  7. $classes[] = 'rtl'; 
  8.  
  9. if ( is_front_page() ) 
  10. $classes[] = 'home'; 
  11. if ( is_home() ) 
  12. $classes[] = 'blog'; 
  13. if ( is_archive() ) 
  14. $classes[] = 'archive'; 
  15. if ( is_date() ) 
  16. $classes[] = 'date'; 
  17. if ( is_search() ) { 
  18. $classes[] = 'search'; 
  19. $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results'; 
  20. if ( is_paged() ) 
  21. $classes[] = 'paged'; 
  22. if ( is_attachment() ) 
  23. $classes[] = 'attachment'; 
  24. if ( is_404() ) 
  25. $classes[] = 'error404'; 
  26.  
  27. if ( is_singular() ) { 
  28. $post_id = $wp_query->get_queried_object_id(); 
  29. $post = $wp_query->get_queried_object(); 
  30. $post_type = $post->post_type; 
  31.  
  32. if ( is_page_template() ) { 
  33. $classes[] = "{$post_type}-template"; 
  34.  
  35. $template_slug = get_page_template_slug( $post_id ); 
  36. $template_parts = explode( '/', $template_slug ); 
  37.  
  38. foreach ( $template_parts as $part ) { 
  39. $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) ); 
  40. $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) ); 
  41. } else { 
  42. $classes[] = "{$post_type}-template-default"; 
  43.  
  44. if ( is_single() ) { 
  45. $classes[] = 'single'; 
  46. if ( isset( $post->post_type ) ) { 
  47. $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id ); 
  48. $classes[] = 'postid-' . $post_id; 
  49.  
  50. // Post Format 
  51. if ( post_type_supports( $post->post_type, 'post-formats' ) ) { 
  52. $post_format = get_post_format( $post->ID ); 
  53.  
  54. if ( $post_format && !is_wp_error($post_format) ) 
  55. $classes[] = 'single-format-' . sanitize_html_class( $post_format ); 
  56. else 
  57. $classes[] = 'single-format-standard'; 
  58.  
  59. if ( is_attachment() ) { 
  60. $mime_type = get_post_mime_type($post_id); 
  61. $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' ); 
  62. $classes[] = 'attachmentid-' . $post_id; 
  63. $classes[] = 'attachment-' . str_replace( $mime_prefix, '', $mime_type ); 
  64. } elseif ( is_page() ) { 
  65. $classes[] = 'page'; 
  66.  
  67. $page_id = $wp_query->get_queried_object_id(); 
  68.  
  69. $post = get_post($page_id); 
  70.  
  71. $classes[] = 'page-id-' . $page_id; 
  72.  
  73. if ( get_pages( array( 'parent' => $page_id, 'number' => 1 ) ) ) { 
  74. $classes[] = 'page-parent'; 
  75.  
  76. if ( $post->post_parent ) { 
  77. $classes[] = 'page-child'; 
  78. $classes[] = 'parent-pageid-' . $post->post_parent; 
  79. } elseif ( is_archive() ) { 
  80. if ( is_post_type_archive() ) { 
  81. $classes[] = 'post-type-archive'; 
  82. $post_type = get_query_var( 'post_type' ); 
  83. if ( is_array( $post_type ) ) 
  84. $post_type = reset( $post_type ); 
  85. $classes[] = 'post-type-archive-' . sanitize_html_class( $post_type ); 
  86. } elseif ( is_author() ) { 
  87. $author = $wp_query->get_queried_object(); 
  88. $classes[] = 'author'; 
  89. if ( isset( $author->user_nicename ) ) { 
  90. $classes[] = 'author-' . sanitize_html_class( $author->user_nicename, $author->ID ); 
  91. $classes[] = 'author-' . $author->ID; 
  92. } elseif ( is_category() ) { 
  93. $cat = $wp_query->get_queried_object(); 
  94. $classes[] = 'category'; 
  95. if ( isset( $cat->term_id ) ) { 
  96. $cat_class = sanitize_html_class( $cat->slug, $cat->term_id ); 
  97. if ( is_numeric( $cat_class ) || ! trim( $cat_class, '-' ) ) { 
  98. $cat_class = $cat->term_id; 
  99.  
  100. $classes[] = 'category-' . $cat_class; 
  101. $classes[] = 'category-' . $cat->term_id; 
  102. } elseif ( is_tag() ) { 
  103. $tag = $wp_query->get_queried_object(); 
  104. $classes[] = 'tag'; 
  105. if ( isset( $tag->term_id ) ) { 
  106. $tag_class = sanitize_html_class( $tag->slug, $tag->term_id ); 
  107. if ( is_numeric( $tag_class ) || ! trim( $tag_class, '-' ) ) { 
  108. $tag_class = $tag->term_id; 
  109.  
  110. $classes[] = 'tag-' . $tag_class; 
  111. $classes[] = 'tag-' . $tag->term_id; 
  112. } elseif ( is_tax() ) { 
  113. $term = $wp_query->get_queried_object(); 
  114. if ( isset( $term->term_id ) ) { 
  115. $term_class = sanitize_html_class( $term->slug, $term->term_id ); 
  116. if ( is_numeric( $term_class ) || ! trim( $term_class, '-' ) ) { 
  117. $term_class = $term->term_id; 
  118.  
  119. $classes[] = 'tax-' . sanitize_html_class( $term->taxonomy ); 
  120. $classes[] = 'term-' . $term_class; 
  121. $classes[] = 'term-' . $term->term_id; 
  122.  
  123. if ( is_user_logged_in() ) 
  124. $classes[] = 'logged-in'; 
  125.  
  126. if ( is_admin_bar_showing() ) { 
  127. $classes[] = 'admin-bar'; 
  128. $classes[] = 'no-customize-support'; 
  129.  
  130. if ( get_background_color() !== get_theme_support( 'custom-background', 'default-color' ) || get_background_image() ) 
  131. $classes[] = 'custom-background'; 
  132.  
  133. if ( has_custom_logo() ) { 
  134. $classes[] = 'wp-custom-logo'; 
  135.  
  136. $page = $wp_query->get( 'page' ); 
  137.  
  138. if ( ! $page || $page < 2 ) 
  139. $page = $wp_query->get( 'paged' ); 
  140.  
  141. if ( $page && $page > 1 && ! is_404() ) { 
  142. $classes[] = 'paged-' . $page; 
  143.  
  144. if ( is_single() ) 
  145. $classes[] = 'single-paged-' . $page; 
  146. elseif ( is_page() ) 
  147. $classes[] = 'page-paged-' . $page; 
  148. elseif ( is_category() ) 
  149. $classes[] = 'category-paged-' . $page; 
  150. elseif ( is_tag() ) 
  151. $classes[] = 'tag-paged-' . $page; 
  152. elseif ( is_date() ) 
  153. $classes[] = 'date-paged-' . $page; 
  154. elseif ( is_author() ) 
  155. $classes[] = 'author-paged-' . $page; 
  156. elseif ( is_search() ) 
  157. $classes[] = 'search-paged-' . $page; 
  158. elseif ( is_post_type_archive() ) 
  159. $classes[] = 'post-type-paged-' . $page; 
  160.  
  161. if ( ! empty( $class ) ) { 
  162. if ( !is_array( $class ) ) 
  163. $class = preg_split( '#\s+#', $class ); 
  164. $classes = array_merge( $classes, $class ); 
  165. } else { 
  166. // Ensure that we always coerce class to being an array. 
  167. $class = array(); 
  168.  
  169. $classes = array_map( 'esc_attr', $classes ); 
  170.  
  171. /** 
  172. * Filters the list of CSS body classes for the current post or page. 
  173. * @since 2.8.0 
  174. * @param array $classes An array of body classes. 
  175. * @param array $class An array of additional classes added to the body. 
  176. */ 
  177. $classes = apply_filters( 'body_class', $classes, $class ); 
  178.  
  179. return array_unique( $classes );