DiggingIntoWordPress

by Chris Coyier & Jeff Starr

Multiple the_date() Functions Return Empty Date

Posted by on

Inside the loop, if you use the function the_date() to display the date the post was published, you may run into trouble. Specifically, if there are two posts published on the same day, the second one will return nothing for a date.

To fix this, use the_time() instead. There is quirk though, as the_time() ouputs, by default, literally a time like “6:02 pm”. You can pass date parameters into it though, and make it output a date instead like the_time(m/d/Y). This means that the date formatting is controlled by your theme though, instead of the Admin area. To get the control back, use this instead:

the_time(get_option('date_format'));

5 Responses

  1. This just happened to me, and I couldn’t figure out why. I’m not super thrilled with the formatting it gives but it will have to work. I’d rather have “2009-10-05″ like i did before. Is this still possible?

    Thanks for posting this

  2. The same-date-disappearing thing is an amusing sense-of-taste Mattism in the WordPress code. You can turn this off (should you want a date to appear next to every post rather than using separate date headings) by doing the following within a template:

    global $previousday; $previousday = -1;

    Then you can just use the usual template functions. Done. :-)

  3. Another option, if you did just want to use the_date()’s output multiple times would be to do this right at the start of The Loop:

    $the_date = the_date('', '', '', FALSE);

    And then wherever you want it, just do:

    echo $the_date;

  4. Joel G Goodman October 26, 2009

    Chris, I saw your tweet about that last week and thought “well yeah, that’s on purpose”. :) I’ve used it a few times.

    It’s sort of like old livejournal style – visually group today’s posts under today’s date.

Comments are closed. Contact us with any critical information. Thank you!

Code is poetry