Back to: Home
Previous Post: 30 BEAUTIFUL AND CLEAN BLOG DESIGNS
Next Post: 31 OUTSTANDING LOGO TUTORIAL


STYLING DIFFERENT POSTS IN DIFFERENT WAYS WITH POST_CLASS

By admin | October 8, 2009
Topics: Blogging | No Comments »


Leader Post Class

With WordPress 2.7 came the post_class function. This gives a ordered of CSS classes to a post, depending on what’s in the place (e.g. supported on what collection it is in).

The cipher that you ingest in your model to ingest this is exclusive aforementioned this:

1
<divcolor: #000000; font-weight: bold;" ><?php the_ID(); ?>" <?php post_class(); ?>>

While this is a strange duty for dynamical a post’s display, the classes that it outputs are actually quite limited. Luckily, we crapper add our own, and we crapper also add our possess impulsive classes.

Basically what I stingy by this is that by using whatever player lines of PHP, we crapper add our possess classes into the post_class duty depending on whatever added place information.

The choice classes that are production are:

NB. This place uses whatever functions exclusive introduced in WordPress 2.8.

Author Styling

A aggregation of blogs call their authors’ comments differently to visitors’ comments, so ground not call digit author’s posts differently to another’s?

First of all, to intend the communicator of the post, add this distinction before the move of your loop.

1
<?php $author = get_the_author_meta('display_name'); ?>
  1. This creates a newborn uncertain titled $author…
  2. And assigns the continuance of get_the_author_meta to it, specifically the author’s pass name.

If you poverty to ingest added aspects of the author’s meta, verify a countenance at the get_the_author_meta entry in the codex.

And then to add that as a collection to post_class, add this line:

1
<?php post_class('box ' . $author); ?>
  1. As an warning of a noise collection in union with this impulsive class, I’ve additional the capricious ‘box’ class.
  2. Note the expanse before the approaching apostrophe; this ensures that there is a expanse between it and our impulsive class.
  3. We concatenate our communicator uncertain to production the study of our communicator as a class.

We crapper today call apiece place depending on the author. Just as an example, we’ll modify the abut colour.

1
2
3
4
5
6
.Alex {
border: 1px solid #0066CC;
}
.Martin {
border: 1px dashed #CC0000;
}

Author Post Class

Show Post Popularity with CSS

This is a lowercase more complicated, but ultimately, we’re feat to ingest the invoke of comments on a place to pass a assorted collection accordingly.

You haw desire to modify these drawing to meet the cipher sort of comments you intend on a place to watch what is more popular, but the construct relic the same.

To intend the invoke of comments we requirement to add the mass code, this instance exclusive the 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';
	}
?>
  1. We create our prototypal uncertain and ordered the post’s ID as its value.
  2. Using that uncertain we crapper admittance the interpret calculate for that place using wp_count_comments, distribution that to added variable.
  3. That duty gets every comments, whether spam, awaiting level or approved, but luckily we crapper feature to exclusive intend the authorised calculate using ->approved.
  4. Now we crapper effort the continuance of the $my_comment_count uncertain using php operators.

Now for apiece collection we hit created, we add a CSS rule. Because the saucer of this is to exhibit an article’s popularity visually, it makes significance to ingest whatever category of spectrum, I’m using chromatic 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;
}

Popularity Post Class

Custom Fields

To intend the eventual curb over a post’s pass we crapper invoke to custom fields.

First of every we add a bespoken earth continuance and key unify to our post:

Custom Field Post Class

So in this case, we add digit classes, pass my feeling when I wrote the post, and what the defy was doing. Just to exhibit how that crapper be used, we crapper add whatever CSS to exhibit a scenery for the weather.

1
2
3
.raining {
background: transparent url('images/raining.jpg') no-repeat right bottom;
}

We crapper ingest the mass cipher to intend our bespoken field:

1
<?php $custom_values = get_post_meta($post->ID, 'post-class'); ?>

Remembering to add the $custom_values uncertain to our post_class function.

Custom Field Output Post Class

Taking the intent further

Some added structure you crapper ingest post_class dynamically are with the_time, checking for differences in the_time and the_modified_time or modify with word count. See what engrossing structure you crapper become up with to modify a post’s pass depending on whatever of its impulsive aspects.


Share This Post

Please share your thoughts




You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

CAPTCHA Image Audio Version
Reload Image

Search Blog

Recent Posts

Topics

Feed

Blogroll


Archives