get_body_class() – Retrieves an array of the class names for the body element.

You appear to be a bot. Output may be restricted

Description

Retrieves an array of the class names for the body element.

Usage

$string[] = get_body_class( $css_class );

Parameters

$css_class
( string|string[] ) optional – Optional. Space-separated string or array of class names to add to the class list. Default empty.

Returns

string[] Array of class names.

Source

File name: wordpress/wp-includes/post-template.php


Lines:

1 to 100 of 235
function get_body_class( $css_class = '' ) {
  global $wp_query;

  $classes = array();

  if ( is_rtl() ) {
    $classes[] = 'rtl';
  }

  if ( is_front_page() ) {
    $classes[] = 'home';
  }
  if ( is_home() ) {
    $classes[] = 'blog';
  }
  if ( is_privacy_policy() ) {
    $classes[] = 'privacy-policy';
  }
  if ( is_archive() ) {
    $classes[] = 'archive';
  }
  if ( is_date() ) {
    $classes[] = 'date';
  }
  if ( is_search() ) {
    $classes[] = 'search';
    $classes[] = $wp_query->posts ? 'search-results' : 'search-no-results';
  }
  if ( is_paged() ) {
    $classes[] = 'paged';
  }
  if ( is_attachment() ) {
    $classes[] = 'attachment';
  }
  if ( is_404() ) {
    $classes[] = 'error404';
  }

  if ( is_singular() ) {
    $post_id   = $wp_query->get_queried_object_id();
    $post      = $wp_query->get_queried_object();
    $post_type = $post->post_type;

    if ( is_page_template() ) {
      $classes[] = "{$post_type}-template";

      $template_slug  = get_page_template_slug( $post_id );
      $template_parts = explode( '/', $template_slug );

      foreach ( $template_parts as $part ) {
        $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) );
      }
      $classes[] = "{$post_type}-template-" . sanitize_html_class( str_replace( '.', '-', $template_slug ) );
    } else {
      $classes[] = "{$post_type}-template-default";
    }

    if ( is_single() ) {
      $classes[] = 'single';
      if ( isset( $post->post_type ) ) {
        $classes[] = 'single-' . sanitize_html_class( $post->post_type, $post_id );
        $classes[] = 'postid-' . $post_id;

        // Post Format.
        if ( post_type_supports( $post->post_type, 'post-formats' ) ) {
          $post_format = get_post_format( $post->ID );

          if ( $post_format && ! is_wp_error( $post_format ) ) {
            $classes[] = 'single-format-' . sanitize_html_class( $post_format );
          } else {
            $classes[] = 'single-format-standard';
          }
        }
      }
    }

    if ( is_attachment() ) {
      $mime_type   = get_post_mime_type( $post_id );
      $mime_prefix = array( 'application/', 'image/', 'text/', 'audio/', 'video/', 'music/' );
      $classes[]   = 'attachmentid-' . $post_id;
      $classes[]   = 'attachment-' . str_replace( $mime_prefix, '', $mime_type );
    } elseif ( is_page() ) {
      $classes[] = 'page';

      $page_id = $wp_query->get_queried_object_id();

      $post = get_post( $page_id );

      $classes[] = 'page-id-' . $page_id;

      if ( get_pages(
        array(
          'parent' => $page_id,
          'number' => 1,
        )
      ) ) {
        $classes[] = 'page-parent';
      }

      if ( $post->post_parent ) {

 View on GitHub View on Trac