Sponsored Link


Sponsored Link

In WordPress 2.8, themes have a new template tag for the body tag which will help theme authors to style more effectively with CSS. The new Template Tag is called body_class. This function gives the body element different classes and can be added, typically, in the header.php’s HTML body tag.


<?php body_class( $class ); ?>


The body_class may include one or more of the following values for the class attribute.

  • rtl
  • home
  • blog
  • archive
  • date
  • search
  • paged
  • attachment
  • error404
  • single postid-(id)
  • attachmentid-(id)
  • attachment-(mime-type)
  • author
  • author-(user_nicename)
  • category
  • category-(slug)
  • tag
  • tag-(slug)
  • page-parent
  • page-child parent-pageid-(id)
  • page-template page-template-(template file name)
  • search-results
  • search-no-results
  • logged-in
  • paged-(page number)
  • single-paged-(page number)
  • page-paged-(page number)
  • category-paged-(page number)
  • tag-paged-(page number)
  • date-paged-(page number)
  • author-paged-(page number)
  • search-paged-(page number)



This article is a ROUGH DRAFT. The author is still working on this document, so please do not edit this without the author’s permission. The content within this article may not yet be verified or valid. This information is subject to change.

Dafault Value
page type (condition) class remarks
If Reading Settings > Front page displays – Your latest posts Main Blog Page (is_front_page, is_home) home blog [ paged paged-n ]
If Reading Settings > Front page displays – A static page selected as “Front page” (is_front_page) home page page-id-ID [*] * see also is_page
selected as “Posts page” (is_home) blog [ paged paged-n ]
is_archive is_author archive author author-user_nicename [ paged paged-n author-paged-n ]
is_category archive category category-slug [ paged paged-n category-paged-n ]
is_tag archive tag tag-slug [ paged paged-n tag-paged-n ]
is_date archive date [ paged paged-n date-paged-n ]
is_search find search search-results [ paged paged-n search-paged-n ]
no results search search-no-results [ paged paged-n search-paged-n ]
is_404 error404
is_single single postid-ID [ paged paged-n single-paged-n]
is_attachment attachment single postid-ID attachmentid-postID attachment-mime-type
is_page page page-id-ID [ page-template page-template-template_file_name ] [ paged paged-n page-paged-n ]
parent page page-id-ID page-parent [ page-template page-template-template_file_name ] [ paged paged-n page-paged-n ]
child page page-id-ID page-child parent-pageid-parent-ID [ page-template page-template-template_file_name ] [ paged paged-n page-paged-n ]
(additional class) is_user_logged_in logged-in
is_paged paged
paged < 2 paged
paged > 1 paged-n page-paged-n
text_direction: rtl rtl



How to pass parameters to tags with PHP function-style parameters


(string or array) (optional) 1つ或いはそれ以上のクラスは、クラス属性に1つのスペースで区切って追加します。

(string or array) (optional) One or more classes to add to the class attribute, separated by a single space.

Default: null



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’ page from a default WordPress installation):

<body class="page page-id-2 page-template 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 */



The classes are restricted to the defaults listed.


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

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


The results would be:

<body class="class-name post post-id-24">


body_class関数のフィルタリング機能によって、body classesを追加することができます。

You can add additional body classes by filtering the body_class function.

あなたのニーズを満たすためにmy_class_names と class-nameを変更して、WordPressテーマのfunctions.phpファイルに次を追加します。

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
function my_class_names($classes, $class) {
	// add 'class-name' to the $classes array
	$classes[] = 'class-name';
	// return the $classes array
	return $classes;


To add a category class to single post pageviews and template files, add the following to the functions.php.

// add category nicenames in body and post class
	function category_id_class($classes) {
	    global $post;
	    foreach((get_the_category($post->ID)) as $category)
	        $classes[] = $category->category_nicename;
	        return $classes;
	add_filter('post_class', 'category_id_class');
	add_filter('body_class', 'category_id_class');



body_class() is located in wp-includes/post-template.php.


Since: 2.8


the_ID, the_title, the_title_attribute, single_post_title, the_title_rss, the_content, the_content_rss, the_excerpt, the_excerpt_rss, wp_link_pages, next_post_link, next_posts_link, previous_post_link, previous_posts_link, posts_nav_link, sticky_class, the_meta

Sponsored Link