Codex

Template Tags/post class

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

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

The post_class() outputs the class="whatever" piece for that div. This includes several different classes of value: post, 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.

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 – .hentry – .sticky – .category-misc – .tag-news – .tag-wordpress – .tag-markup


See also

Template_Tags/comment_class