Quick WordPress Tip: Style Posts According to Date

Ever wanted to give a post element a different look and feel (or even include/exclude content) based on a post’s publication date? I got asked about this earlier today, and I figured it would be a handy tip to share.

I don’t claim to be a PHP coder, so while it does work, it could be that this can be shortened somewhat. Feel free to use it as a base for making it more efficient, if you’d like (and if you can, I’d love to see what you come up with!).

OK… Somewhere within the Loop, before the area you want to customise, enter the following:

<?php $postdate = the_date('Y-m-d','','',false) ?>

That tells WP to find out the publication date of a post and to assign it to the function, $postdate. You can then call that function later in the post. For example:

<p<?php if ($postdate < date("2008-12-29")) { echo ' class="old"'; } ?>>

That snippet of code will check to see if the post was published after 29th December, 2008. If not, it will be assigned the “old” class, and you can style that however you like through your stylesheet.

Of course, it’s not limited to CSS styling — you can place whatever you like within the if statement, so you could, say, choose to show full posts after a certain date, and excerpts of posts written earlier.

Comments on Quick WordPress Tip: Style Posts According to Date

ReTodd

Nice. I haven’t had a use for this yet, but now that you’ve posted it, I may have to add it to my arsenal for the next redesign. Thanks

Posted by ReTodd (1 comments) on 6th March, 2009 at 5:25 pm.

kelvinwins.com

Nice post. Handy stuff to know. Thanks.

Posted by kelvinwins.com (1 comments) on 6th March, 2009 at 5:31 pm.

Rob Barrett

Thanks guys. Yeah, it’s not something that’s likely to be widely needed, but I do think it’s cool that it’s possible to have this kind of functionality in WordPress for the rare occasions that you need to to do slightly more complex stuff.

Posted by Rob Barrett (45 comments) on 15th March, 2009 at 1:32 am.

Dish Network

I want to implement this snippet so I can distinguish posts by date originated.

Posted by Dish Network (1 comments) on 12th July, 2009 at 12:17 pm.

Rob Barrett

Dish Network: This will work for that. Just enter the first snippet into the Loop, and $postdate will output the publication date of each post.

Posted by Rob Barrett (45 comments) on 12th July, 2009 at 4:19 pm.

Furniture Lift

Hi Rob,

Thanks for sharing this information. I getting to love your blog because I can get lot of tips and tutorials that are easy to learn and do. Keep it up because your helping lot of blogger and even simple peoples.

Posted by Furniture Lift (4 comments) on 28th February, 2010 at 3:16 pm.

ohare airport limo service

It may not be widely needed but I think it is a very great tip. Not every one can come up with like this. For me you are a great designer and with a big heart to share every thing you have learned.

Posted by ohare airport limo service (7 comments) on 15th April, 2010 at 10:48 am.

Leave a Comment





Rob Barrett Introduction