How to Style the Date in the Genesis Post Info

If you want to add some fancy styling to the post date to make it stand out, then there’s two things we need to do. The first is to alter the format of the date and add extra markup, and the second is to add some CSS to our stylesheet. Take a look at the following screenshot showing the post title and post info, and take note of the fairly bland appearance of the date:

Screenshot of the unstyled original date

The original date - fairly standard and non-interesting

That’s not doing it for me, so lets go and jazz it up a little.


The PHP Code

Open up your child theme’s functions.php file and add the following at the bottom, just before any closing ?> that might be there:

Explanation of the PHP Code

The first line is simply a comment, so we’ll know in the future what this bit of code does.

The add_filter() on the second line tells WordPress, that whenever the Genesis 2010-08-27 shortcode is used (as it is in some child theme bylines), we want to alter it with some of our own changes. We want it to run at normal (10) priority, and we’ll be passing in two variables to our custom function.

The rest of the code is the function – the long line is the markup we’ll be returning and outputting. The first value is the markup with a few placeholders (e.g. %1$s), and the next 4 values are the contents of those placeholders. There’s two main things we’re achieving here:

  • We’re splitting the date into two bits, and formatting them as simply the day of the month (j), and the 3-letter month (M).
  • We’re adding ... and ... around the date parts.

All of this gives us the following:

Screenshot of the altered date, with extra markup

The altered date, with extra markup

It’s different, but not quite what we were after.

Suggested CSS

We need to add some styling to our child theme’s style.css to really make it effective. Here’s some suggested styles, but feel free to come up with your own variations.

Explanation of the CSS Code

Most of it should be fairly obvious, but there are a couple of things to note that highlights the technique we’re using here.

  • The .post-info has a left padding of 60px, so that the “by Cheryl” bit is shifted over to the right and isn’t over the top of our date. The post title (whether it appears as a h1 or h2 level heading, so long as it’s for a post and not a page which has no date) has a margin of the same distance so it starts further right too.
  • The .time element is made into a block, given a width, and positioned absolutely 10 pixels from the top and left of the relatively positioned .post element.
  • The .day and .month elements are also made into block elements with a given width, then styled with a background and some text formatting to suit.

With all of that styling in place, our date now looks like this:

The finished styled date

By using absolute positioning, instead of negative margins to pull the date block left and up into position, then should the post title wrap around to the next line, the date block won't be forced down a line.

Conclusion

This is a great way of making your post date information stand out, yet still maintain aspects of your design. Instead of plain background colours, you could add a background image instead, add rounded corners, or incorporate the year. Now you know how to add additional markup, then the rest is up to you!

Comments

  1. Erin says:

    Where is the css code? The php code is shown where the css code should be. The is exactly what I’m looking for but the css code would be helpful in achieving it. Thanks.

  2. Erin says:

    Thank you. :)

  3. Albert says:

    Hi, Gary. I ran thru your great post on styling date which seem very helpful to me. I am modifying a theme to display this style, but I fail. I created a div for the Date and Month using the following code, but it doesn’t work ,it output nothing, can you please help to check for me what’s the problem?

    Firstly I just need to have the date and month in seperative span markup so that I can style them with CSS. I know nothing about PHP code but have some basic knowledge of CSS. Thank you very much.

    • Gary says:

      Hi Albert – your comment code got eaten, so can you paste your code at pastebin.com (or equivalent) and drop the link here instead?

  4. Katie says:

    I tried pasting the code into my functions.php file and got an error.

    Something I’ve done wrong?

  5. Katie says:

    It said it wasn’t configured properly and I had to go in to my hosting to take it out (I was working in WP back end through the editor.).

    Maybe I didn’t put it in the proper location within the functions.php?

    • Gary says:

      If it was something to do with the code, it would have given a file name and line number that the error is related to.

      So long as the code goes after the opening <?php and not inside any other function, it should be fine.

      Also, *NEVER* edit PHP via the evil WP editor – for the very reason you’ve just found out.

  6. Katie says:

    Lesson learned. I did it through my hosting backend the same way I had did it previously and this time it worked. Now time to play with the CSS.

    Thanks Gary!
    Cheers.

  7. Thanks for this article on styling Date in Genesis, really useful and working !

  8. MWright says:

    Great tutorial on styling date, time in the Genesis post info.

    Thanks Gary

    Mike

Speak Your Mind

*