返回介绍

body_class()

发布于 2017-09-10 21:35:18 字数 7654 浏览 1326 评论 0 收藏 0

body_class( string|array $class = '' )

Display the classes for the body element.


description


参数

$class

(string|array) (Optional) One or more classes to add to the class list.

Default value: ''


源代码

File: wp-includes/post-template.php

function body_class( $class = '' ) {
	// Separates classes with a single space, collates classes for body element
	echo 'class="' . join( ' ', get_body_class( $class ) ) . '"';
}

更新日志

Versiondescription
2.8.0Introduced.

More Information

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

Basic Usage

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 like this (the About the Tests page from the Theme Unit Test):


<body class="page page-id-2 page-parent page-template-default logged-in">

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


.page {
	/* styles for all posts within the page class */
}
.page-id-2 {
	/* styles for only page ID number 2 */
}
.logged-in {
	/* styles for all pageviews when the user is logged in */
}

相关函数

Uses

  • wp-includes/post-template.php: get_body_class()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 7You must log in to vote on the helpfulness of this note Contributed by Drew Jaynes

    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 results would be:

    
    <body class="page page-id-2 page-parent page-template-default logged-in class-name">
    
  2. Add New Classes via Filter

    You can add additional body classes by filtering the {@see ‘body_class’} hook.

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

    
    // Add specific CSS class by filter.
    
    add_filter( 'body_class', function( $classes ) {
    	return array_merge( $classes, array( 'class-name' ) );
    } );
    

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

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

    The result on the front-end:

    <body class="page page-id-7 page-template page-template-page-example page-template-page-example-php example">

    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;
    });
    function wp_body_classes( $classes ) {
        $classes[] = 'class-name';
         
        return $classes;
    }
    add_filter( 'body_class','wp_body_classes' );

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

    .rtl {
    	/*

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

    
    .single-/* sanitize_html_class($post->post_type, $post_id) */
    
    .postid-/* $post_id */
    
    .single-format-/* sanitize_html_class( $post_format ) */
    
    .attachmentid-/* $post_id */
    
    .attachment-/* str_replace( $mime_prefix, '', $mime_type ) */
    
    .post-type-archive-/* sanitize_html_class( $post_type ) */
    
    .author-/* sanitize_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-/* sanitize_html_class( $term->taxonomy ) */
    
    .term-/* $term_class */
    
    .term-/* $term->term_id */
    
    .page-id-/* $page_id */
    
    .parent-pageid-/* $post->post_parent */
    
    .page-template-/* sanitize_html_class( str_replace( array( '.', '/' ), '-', basename( $part, '.php' ) ) ) */
    
    .page-template-/* sanitize_html_class( str_replace( '.', '-', $template_slug ) ) */
    
    .paged-/* $page */
    
    .single-paged-/* $page */
    
    .page-paged-/* $page */
    
    .category-paged-/* $page */
    
    .tag-paged-/* $page */
    
    .date-paged-/* $page */
    
    .author-paged-/* $page */
    
    .search-paged-/* $page */
    
    .post-type-paged-/* $page */
    

    You can check all these in function get_body_class()

    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;
    } );
    

    Adding maijabrazen. 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-'.get_the_author_meta('ID') ;
        if (in_array($author, $classes)) {
          unset( $classes[array_search($author, $classes)] );
        }
      return $classes;

    });

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文