

With WordPress 2.7 came a post_class function. This gives a set of CSS classes to a post, depending on what’s in a post (e.g. formed on what difficulty it is in).
The formula which we make make make make make make make use of of of of of of of in your template to make make make make make make make use of of of of of of of this is simply similar to this:
1
|
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
|
While this is a illusory duty for becoming opposite a post’s display, a classes which it outputs have been essentially utterly limited. Luckily, we can supplement a own, as well as we can additionally supplement a own energetic classes.
Basically what we meant by this is which by regulating a small additional lines of PHP, we can supplement a own classes in to a post_class duty depending on a small alternative post information.
The default classes which have been outlay are:
- post-id, where id is a post’s id
- post type, which by default have been post, page as well as attachment
- sticky, for posts noted as gummy on a post write panel
- hentry, for hAtom compliance
- category-category-name, so if your difficulty was wordpress, it would be category-wordpress
- tag-tag-name, so if your tab was php, it would be tag-php
NB. This post uses a small functions usually introduced in WordPress 2.8.
Author Styling
A lot of blogs character their authors’ comments otherwise to visitors’ comments, so because not character a single author’s posts otherwise to another’s?
First of all, to get a writer of a post, supplement this line prior to a begin of your loop.
1
|
<?php $author = get_the_author_meta('display_name'); ?>
|
- This creates a brand new non-static called $author…
- And assigns a worth of get_the_author_meta to it, privately a author’s arrangement name.
If we instruct to make make make make make make make use of of of of of of of alternative aspects of a author’s meta, take a demeanour at a get_the_author_meta entrance in a codex.
And afterwards to supplement which as a category to post_class, supplement this line:
1
|
<?php post_class('box ' . $author); ?>
|
- As an e.g. of a immobile category in as well as with this energetic class, I’ve combined a capricious ‘box’ class.
- Note a space prior to a shutting apostrophe; this ensures which there is a space in between it as well as a energetic class.
- We concatenate a writer non-static to outlay a name of a writer as a class.
We can right away character any post depending on a author. Just as an example, we’ll shift a limit colour.
1
2
3
4
5
6
|
.Alex {
border: 1px solid #0066CC;
}
.Martin {
border: 1px dashed #CC0000;
}
|

Show Post Popularity with CSS
This is a small some-more complicated, though ultimately, we’re starting to make make make make make make make use of of of of of of of a volume of comments on a post to furnish a opposite category accordingly.
You might instruct to shift these numbers to fit a normal series of comments we get on a post to establish what is some-more popular, though a judgment stays a same.
To get a volume of comments we need to supplement a following code, this time inside a loop.
1
2
3
4
5
6
7
8
9
10
11
12
|
<?php
$postid = get_the_ID();
$total_comment_count = wp_count_comments($postid);
$my_comment_count = $total_comment_count->approved;
if ($my_comment_count <10) {
$my_comment_count = 'new';
} elseif ($my_comment_count >= 10 && $my_comment_count <20) {
$my_comment_count = 'ermerging';
} elseif ($my_comment_count >= 20) {
$my_comment_count = 'popular';
}
?>
|
- We emanate a initial non-static as well as set a post’s ID as a value.
- Using which non-static we can entrance a criticism equate for which post regulating wp_count_comments, assigning which to an additional variable.
- That duty gets all comments, either spam, available mediation or approved, though luckily we can contend to usually get a authorized equate regulating
->approved.
- Now we can exam a worth of a
$my_comment_count non-static regulating php operators.
Now for any category we have created, we supplement a CSS rule. Because a indicate of this is to uncover an article’s recognition visually, it creates clarity to make make make make make make make use of of of of of of of a small kind of spectrum, I’m regulating yellow to red.
1
2
3
4
5
6
7
8
9
|
.new {
border: 1px solid #FFFF00;
}
.emerging {
border: 1px dashed #FF9933;
}
.popular {
border: 1px dashed #CC0000;
}
|

Custom Fields
To get a idealisation carry out over a post’s arrangement we can spin to custom fields.
First of all we supplement a law margin worth as well as pass span to a post:

So in this case, we supplement dual classes, arrangement my mood when we wrote a post, as well as what a continue was doing. Just to uncover how which can be used, we can supplement a small CSS to uncover a credentials for a weather.
1
2
3
|
.raining {
background: transparent url('images/raining.jpg') no-repeat right bottom;
}
|
We can make make make make make make make use of of of of of of of a following formula to get a law field:
1
|
<?php $custom_values = get_post_meta($post->ID, 'post-class'); ?>
|
Remembering to supplement a $custom_values non-static to a post_class function.

Taking a thought further
Some alternative ways we can make make make make make make make use of of of of of of of post_class boldly have been with the_time, checking for differences in the_time as well as the_modified_time or even with word count. See what engaging ways we can come up with to shift a post’s arrangement depending on a small of a energetic aspects.


See a strange post:
Styling Different Posts in Different Ways With Post_Class