• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

テンプレートタグ/post class

提供: WordPress Codex 日本語版
< テンプレートタグ
2010年11月12日 (金) 23:14時点におけるBono (トーク | 投稿記録)による版 (テンプレートタグガイド挿入、リンク・カテゴリ等調整/翻訳者募集中)

移動先: 案内検索

このページ「テンプレートタグ/post class」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

Description

WordPress Themes have a template tag for the post HMTL tag which will help theme authors to style more effectively with CSS. The Template Tag is called post_class. This function prints out different post container classes which can be added, typically, in the index.php, single.php, and other template files featuring post content, typically in the HTML <div id="post"> tag. If you don't want the post classes to be echoed use get_post_class()/en.

The post_class template tag creates CSS selectors to style content only within the post content area. For information on styling entire pages based upon a distinctive CSS selector, see テンプレートタグ/body_class.

Usage

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

The post_class may include one or more of the following values for the class attribute, dependent upon the pageview.

  • .post-id
  • .post
  • .attachment
  • .sticky
  • .hentry (hAtom microformat pages)
  • .category-ID
  • .category-name
  • .tag-name

Default Values

The post_class CSS classes appear based upon the post pageview Conditional Tags as follows.

Front Page
If posts are found on the front page of the blog, be it static or not, the class selectors are: post post-id hentry

Single Post
Single post template files and pageviews feature the class selectors: post post-id hentry

Sticky Post
Posts designated as "sticky," sticking to the front page of the blog, feature the class selector: sticky

Author
Author template files and pageviews displaying posts feature the class selectors: post post-id

Category
Category template files and pageviews displaying posts feature the class selectors: post post-id category-ID category-name

Tags
Tag template files and pageviews with posts feature the class selectors: tag-name post post-id

Archives
Archive pageviews and pageviews with posts feature CSS classes: post post-id

Search
Search template files and pageviews with posts feature the class selectors: post post-id

Attachment
Attachment template files and pageviews feature the class selectors: attachment post post-id

Parameters

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

Examples

Implementation

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

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

The actual HTML output might resemble something like this for a post in the "dancing" category:

<div id="post-4564" class="post post-4564 category-48 category-dancing logged-in">

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

.post {
	    /* styles for all posts */
	    }
.post-4564 {
	    /* styles for only post ID number 4564 */
	    }
.category-dancing {
	    /* styles for all posts within the category of dancing */

Adding More Classes

The classes are restricted to the defaults listed. To add more classes to the post content area, the template tag's parameter can be added. For example, to add a unique class to any template file:

<div id="post-<?php the_ID(); ?>" <?php post_class('class-name'); ?>>

The results would be:

<div id="post-4564" class="class-name post post-4564 category-48 category-dancing logged-in">

Add Classes By Filters

To add a category class to single post pageviews and template files in the post content area for the post class and the entire page in the body HTML class, 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');

Display Posts Outside of the Loop

For displaying posts outside the Loop or in an alternate Loop, the second parameter to the post_class function can be the post ID. Classes will then be determined from that post.

<?php post_class('',$post_id); ?>

Source Code

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

Changelog

最新英語版: WordPress Codex » Template Tags/post class最新版との差分

Related

投稿タグ: body_class(), next_image_link(), next_post_link(), next_posts_link(), post_class(), post_password_required(), posts_nav_link(), previous_image_link(), previous_post_link(), previous_posts_link(), single_post_title(), sticky_class(), the_category(), the_category_rss(), the_content(), the_content_rss(), the_excerpt(), the_excerpt_rss(), the_ID(), the_meta(), the_shortlink(), the_tags(), the_title(), the_title_attribute(), the_title_rss(), wp_link_pages(),テンプレートタグ/comment_class/en


テンプレートタグへの PHP 関数型パラメータの渡し方関数リファレンステンプレートタグ目次もご覧ください。