Sponsored Link


Sponsored Link

WordPress 2.7 includes a new function for post classes, which will help theme authors perform simpler styling. The function is, appropriately enough, called post_class().


To use this function in a theme, you will simply add it to the Loop in a place that makes sense. Most themes encapsulate every post within a DIV of some sort. That DIV usually has a class=”post” or something similar. Instead of that class, just add a call to post_class instead. Like so:

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

post_class()はdivの場所にclass=”whatever”を出力します。この値は、いくつかの異なるクラスを含みます:post-123(数値post ID)、記事、ページ、添付ファイル(投稿タイプ)は、hentry(Atomのmicroformatのページ)、category-X(ここでのXは、すべての投稿に含まれるカテゴリー記事のスラッグです)、そしてtag-X(シンプルですが、タグ付け)。また、付箋記事としてマークした投稿に”付箋”を追加します。これらは様々な方法でテーマの部分別にスタイルを簡単に作成します。

The post_class() outputs the class=”whatever” piece for that div. This includes several different classes of value: post-123 (the numerical post ID), post or page or attachment (the post type), hentry (for hAtom microformat pages), category-X (where X is the slug of every category the post is in), and tag-X (similar, but with tags). It also adds “sticky” for posts marked as sticky posts. These make it easy to style different parts of the theme in different ways.


For special cases where you want to add your own classes, post_class supports that too:

<?php post_class('special'); ?>


This will add “special” to the class list. You can either give it a space separated list of classes, or an array of strings with one class each, if your code is more complex and that is more useful. When adding additional classes, WordPress does not check for duplicates.

代替ループ内またはループ外で記事を表示するために、post_class関数への2番目のパラメータではpost IDにすることができます。クラスは、その投稿から決定されます。

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


It can add the following classes which you can use as a hook for CSS-styling:

  • .post-id
  • .post (or .page or .attachment, depending on the post type)
  • .sticky
  • .hentry
  • .category-misc
  • .category-example
  • .tag-news
  • .tag-wordpress
  • .tag-markup




This page is marked as incomplete. You can help Codex by expanding it.

Sponsored Link