body_class()

最后更新于:2021-11-25 20:23:44

body_class( string|string[]$class=”)

Displays the class names for the body element.

参数

$class

(string|string[]) (Optional) Space-separated string or array of class names to add to the class list.

Default value: ”

源文件

文件: gc-includes/post-template.php

function body_class( $class = '' ) {
	// Separates class names with a single space, collates class names for body element.
	echo 'class="' . esc_attr( implode( ' ', get_body_class( $class ) ) ) . '"';
}
.rtl {
	/* # Checks if current locale is RTL (Right To Left script). */
}

.home {
	/* # Depends on the site’s “Front page displays” Reading Settings ‘show_on_front’ and ‘page_on_front’. n If you set a static page for the front page of your site, this function will return true when viewing that page. */
}

.blog {
	/* # Add if blog view homepage, otherwise false. */
}

.archive {
	/* For # Month, Year, Category, Author, Post Type archive */
}

.date {
	/* # For date archive */
}

.search {
	/* # For search */
}

.search-results {
	/* # If found posts in search result */
}

.search-no-results {
	/* # If NOT found any posts in search result */
}

.paged {
	/* # On paged result and not for the first page */
}

.attachment {
	/* # On attachment page */
}

.error404 {
	/* # On 404 page */
}

.single {
	/* # Add for any post type, except {attachments} and {pages} */
}

.single-format-standard {
	/* # standard post format */
}

.post-type-archive {
	/* # post type archive page */
}

.author {
	/* # author page */
}

.category {
	/* # category page */
}

.tag {
	/* # Tags page */
}
	
.page {
	/* # existing single page */
}

.page-parent {
	/* # Parent page only */
}

.page-child {
	/* # Child page only */
}

.page-template {
	/* # Page templates only */
}

.page-template-default {
	/* # Default page templates only */
}

.logged-in {
	/* # Logged in user */
}

.admin-bar {
	/* # Only in admin bar */
}

.no-customize-support {
	/* # Only in admin bar */
}

.custom-background {
	/* # If theme support 'custom-background' or get_background_image() */
}

.gc-custom-logo {
	/* # If the site has a custom logo. */
}
.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 */

// Add Body Class to your custom template

add_filter( 'body_class', 'gcdocs_sp_body_class' );
function gcdocs_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_page_template( $templates ) ) { 
		$classes[] = 'your-custom-class'; // add your class here
	}

	return $classes;
}