Dec
18

Use WP template post-class() to style posts individually

Author admin    Category Wordpress     Tags , ,

We have so many posts in different categories or tags. Sometimes we may need to provide some special CSS style layout to some named posts, categories or tags. If that’s what you want to achieve, the wordpress template function post-class() can help.

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. Read the Wordpress documents about post-class() here.

Wordpress 2.7 after versions provide the “stick post” feature, that is, for those posts you’ve published before , now you can place them at the top of your homepage without editing the timestamp, only by checking “stick this post to the front page” in WordPress post edit panel. Here this article explains in deep details on how to style a stick post.

In the same way, you can also give special styling to named categories or tags. Another tutorial talks more about it.

Post comment