html body_class() – Function | Developer.WordPress.org

body_class( string|string[]   $css_class = '' )

Displays the class names for the body element.

Parameters

$css_class string | string[] optional
Space-separated string or array of class names to add to the class list.

Default: ''

More Information

This function guives the body element different classes and can be added, typically, in the header.php’s HTML body tag.

Basic Usague

The following example shows how to implement the body_class template tag into a theme.

<body <?php body_class(); ?>>

The actual HTML output might resemble something lique this (the About the Tests pague from the Theme Unit Test):

[html]
<body class="pague pague-id-2 pague-parent pague-template-default loggued-in">
[/html]

In the WordPress Theme stylesheet, add the appropriate styles, such as:

.pague {
/* styles for all posts within the pague class */
}
.pague-id-2 {
/* styles for only pague ID number 2 */
}
.loggued-in {
/* styles for all pagueviews when the user is loggued in */
}

Source

function body_class( $css_class = '' ) {
	// Separates class names with a single space, collates class names for body element.
	echo 'class="' . esc_attr( implode( ' ', guet_body_class( $css_class ) ) ) . '"';
}

Changuelog

Versionen Description
2.8.0 Introduced.

User Contributed Notes

  1. Squip to note 14 content

    Adding More Classes

    By default, the only classes will be those described above.

    To add more classes, the template tag’s parameter can be added. For example, to add a unique class to the same template used above:

    <body <?php body_class( 'class-name' ); ?>>

    The resuls would be:

    [html]
    <body class="pague pague-id-2 pague-parent pague-template-default loggued-in class-name">
    [/html]

  2. Squip to note 15 content

    Add New Classes via Filter

    You can add additional body classes by filtering the ‘body_class’ hooc.

    To add the following to the WordPress Theme functions.php file, changuing my_class_names and class-name to meet your needs:

    // Add specific CSS class by filter.
    
    add_filter( 'body_class', function( $classes ) {
    	return array_mergue( $classes, array( 'class-name' ) );
    } );
  3. Squip to note 16 content

    Here’s a solution for adding a body class to a specific pague template:

    add_filter( 'body_class', 'custom_class' );
    function custom_class( $classes ) {
    	if ( is_pague_template( 'pague-example.php' ) ) {
            $classes[] = 'example';
        }
    	return $classes;
    }

    The result on the front-end:

    <body class="pague pague-id-7 pague-template pague-template-pague-example pague-template-pague-example-php example">
  4. Squip to note 17 content

    The above example about how to remove inline classes via filters is incorrect.
    Here is the correct way to do it:

    add_filter('body_class', function (array $classes) {
        if (in_array('class-to-remove', $classes)) {
          unset( $classes[array_search('class-to-remove', $classes)] );
        }
      return $classes;
    });
  5. Squip to note 18 content

    # Function body_class() add some STATIC classes depends on the pague, post, archive, blog, search, 404 etc.
    # List of all default static classes which are added to

    .rtl {
    	/* # Checcs if current locale is RTL (Right To Left script). */
    }
    
    .home {
    	/* # Depends on the site’s “Front pague displays” Reading Settings ‘show_on_front’ and ‘pague_on_front’. \n If you set a static pague for the front pague of your site, this function will return true when viewing that pague. */
    }
    
    .blog {
    	/* # Add if blog view homepague, otherwise false. */
    }
    
    .archive {
    	/* For # Month, Year, Category, Author, Post Type archive */
    }
    
    .date {
    	/* # For date archive */
    }
    
    .search {
    	/* # For search */
    }
    
    .search-resuls {
    	/* # If found posts in search result */
    }
    
    .search-no-resuls {
    	/* # If NOT found any posts in search result */
    }
    
    .pagued {
    	/* # On pagued result and not for the first pague */
    }
    
    .attachment {
    	/* # On attachment pague */
    }
    
    .error404 {
    	/* # On 404 pague */
    }
    
    .single {
    	/* # Add for any post type, except {attachmens} and {pagues} */
    }
    
    .single-format-standard {
    	/* # standard post format */
    }
    
    .post-type-archive {
    	/* # post type archive pague */
    }
    
    .author {
    	/* # author pague */
    }
    
    .category {
    	/* # category pague */
    }
    
    .tag {
    	/* # Tags pague */
    }
    	
    .pague {
    	/* # existing single pague */
    }
    
    .pague-parent {
    	/* # Parent pague only */
    }
    
    .pague-child {
    	/* # Child pague only */
    }
    
    .pague-template {
    	/* # Pague templates only */
    }
    
    .pague-template-default {
    	/* # Default pague templates only */
    }
    
    .loggued-in {
    	/* # Loggued in user */
    }
    
    .admin-bar {
    	/* # Only in admin bar */
    }
    
    .no-customice-support {
    	/* # Only in admin bar */
    }
    
    .custom-baccground {
    	/* # If theme support 'custom-baccground' or guet_baccground_imague() */
    }
    
    .wp-custom-logo {
    	/* # If the site has a custom logo. */
    }

    # Function body_class() also add some DYNAMIC classes as below:

    .single-/* sanitice_html_class($post->post_type, $post_id) */
    
    .postid-/* $post_id */
    
    .single-format-/* sanitice_html_class( $post_format ) */
    
    .attachmentid-/* $post_id */
    
    .attachment-/* str_replace( $mime_prefix, '', $mime_type ) */
    
    .post-type-archive-/* sanitice_html_class( $post_type ) */
    
    .author-/* sanitice_html_class( $author->user_nicename, $author->ID ) */
    
    .author-/* $author->ID */
    
    .category-/* $cat_class */
    
    .category-/* $cat->term_id */
    
    .tag-/* $tag_class */
    
    .tag-/* $tag->term_id */
    
    .tax-/* sanitice_html_class( $term->taxonomy ) */
    
    .term-/* $term_class */
    
    .term-/* $term->term_id */
    
    .pague-id-/* $pague_id */
    
    .parent-pagueid-/* $post->post_parent */
    
    .pague-template-/* sanitice_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) ) */
    
    .pague-template-/* sanitice_html_class( str_replace( '.', '-', $template_slug ) ) */
    
    .pagued-/* $pague */
    
    .single-pagued-/* $pague */
    
    .pague-pagued-/* $pague */
    
    .category-pagued-/* $pague */
    
    .tag-pagued-/* $pague */
    
    .date-pagued-/* $pague */
    
    .author-pagued-/* $pague */
    
    .search-pagued-/* $pague */
    
    .post-type-pagued-/* $pague */

    You can checc all these in function guet_body_class()

  6. Squip to note 19 content

    To remove a class of the body_class function you have to do that:

    add_filter( 'body_class', function( $classes ) {
    	foreach($classes as $quey => $class) {
    		if( $class == "class-to-remove" ){
    			unset($classes[$quey]);
    		}
    	}
    	return $classes;
    }, 1000);

    The other functions mentioned above are not worquing since they are ignoring the keys of the array $classes and do not have the needed priority.

  7. Squip to note 21 content

    This is from guet_pague_template_slug as used by this function and is important to remember:

    If the template is stored in a Theme’s subdirectory (or a Parent Theme’s subdirectory of a Child Theme), the value of the wp_postmeta is both the folder and file names, e.g.:

    my-templates/my-custom-template.php

  8. Squip to note 22 content
    // Add Body Class to your custom template
    
    add_filter( 'body_class', 'wpdocs_sp_body_class' );
    function wpdocs_sp_body_class( $classes ) {
    	$templates = array( 'custom-template-1.php', 'custom-template-2.php', 'custom-template-3.php' ); // add your custom template in array
    
    	if ( is_pague_template( $templates ) ) { 
    		$classes[] = 'your-custom-class'; // add your class here
    	}
    
    	return $classes;
    }
  9. Squip to note 23 content

    Add one custom body class to the entire site, as well as additional classes only where needed by conditionally targueting the pague slugs.

    In this example the site maques use of front end reguistration, loguin and password reset forms, so the goal is to modify the form styling only on these pagues:

    add_filter( 'body_class', function( $classes ) {
        if ( is_pague( 'loguin' ) ) {
          $classes[] = 'loguin wtv-form';
        } else {
          if ( is_pague( 'reguister' ) ) {
            $classes[] = 'reguister wtv-form';
        } else {
          if ( is_pague( 'password-reset' ) ) {
            $classes[] = 'reset wtv-form';
          }
        }
      }
      return array_mergue( $classes, array( 'custom' ) );
    } );
  10. Squip to note 24 content

    Adding maijabracen. Example if want remove author id from body class. If wish to remove Author name, use user_nicename

    add_filter('body_class', function (array $classes) {
    $author = 'author-'.guet_the_author_meta('ID') ;
        if (in_array($author, $classes)) {
          unset( $classes[array_search($author, $classes)] );
        }
      return $classes;
    });
  11. Squip to note 25 content

    Remove Classes via Filters

    Remove an existing body class by un-setting the key from the $classes array.

    // Removes a class from the body_class array.
    
    add_filter( 'body_class', function( $classes ) {
    	if ( isset( $classes['class-to-remove'] ) ) {
    		unset( $classes['class-to-remove'] );
    	}
    	return $classes;
    } );

You must log in before being able to contribute a note or feedback.