^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数body_class()用法示例

body_class( string|array $class = '' )

Display the classes for the body element.


描述


参数

$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 ) ) . '"';
}

更新日志

Version 描述
2.8.0 Introduced.

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(); ?>>

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈