WordPress: Customize CSS if the Post Published Today

wordpress logo

I've been wanting to add the little calendar graphics to my posts for a while now. I wrote two classes for the date div and set the background image differently based on whether or not the post was written today. Thanks to Michael H in the WordPress Support Forums, I finally got my statement correct! Here's what I did. I have a background image set for the div class date:

<div class="the_date">

For today's div, I set a different background image applied to a div class named the_date_today:

<div class="the_date_today">

Now that I've got those set, I need to write some code that adds the “_today” if the post was written today:

<div class="the_date<?php $post_date = mysql2date('Ymd', $post->post_date_gmt); if($post_date==gmdate('Ymd')) { echo '_today'; } ?>">

Here's how this works:

  1. I set a variable called $post_date equal to the date of the post formatted as Ymd.
  2. I write an if statement so that if that variable equals today's date (formatted as Ymd as well), I add “_today”

Voila! Now I have a calendar graphic that displays whether or not the post was written today! I just need to adjust for timezone and I'll have it made!


  1. 1

    Hey Doug. That’s really slick!

    Side note, I suggest you move your ‘subscribe to’ check box above the add comment button… to me that’s a little more user friendly.

    Great job on your new calendar graphics and CSS.

    • 2

      Thanks Sean.

      The positioning of the check box is on purpose. Putting it outside the other fields would create separation between it and the other tightly spaced fields. By placing it near the button, it’s putting a selection near an action, this actually might cause more people to miss it as they complete their thoughts in a comment and move to submit.

      One thing that is missing is proper tab stops, though. I am going to correct that.

  2. 3

    Well I think there is a bug in your code now that it’s a new day. The calendar icon still says today but it’s actually tomorrow now 🙂

  3. 5

    Ok, I didn’t realize that’s what you were meaning about adjusting for GMT.

    I’m sure you’re on top of it mr code monkey 🙂 but maybe you could do some sort of ‘if’ statement looking at your server time?

    if server date/time is X compared to post date/time show X image or something to that effect.

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.