How to show each post’s date in WordPress

clockThe templating system in WordPress is very flexible, and there’s rarely something you can’t do or that doesn’t work as you’d expect. A notable exception however is the_date. Its purpose is simple enough. It displays the current post’s date of creation. But on pages with more than one post (such as on many blog home pages), something weird happens. If a number of posts on any given page were created on the same date, the_date will only show that date for the first of those posts.

In the early days of blogging, posts were usually listed by date, much like a regular, paper diary. In the old default theme that comes with WordPress a big date title is used to separate posts into days. That’s what the_date was created to do, and so it makes sense it only displays the same date once. In most modern themes however, people like the date to be among the meta data for each article, so the_date falls short.

the_time

In comes the_time. This template tag, as the name suggests, shows the time at which a post was created. However, it accepts a PHP time format string as an argument, and can be made to display the date. You could simply put a date formatting string in there and be done with it, but I like to keep things flexible, so i wanted to use the date format string set in WordPress’ settings. That way the user controls the output, and it can be adjusted to international preferences.

When I first ran into this I posted my issue on the WordPress forums, and Otto42 came up with a prefect solution. I’ve been using it ever since and thought I’d share it here.

the_time(get_option('date_format'));

What this does is get the user-set date format from WordPress and feed it to the_time. Since that function doesn’t have a ‘once a day’ limitation, you can use this as often as you like in your theme.

Share and Enjoy:
  • Digg
  • Slashdot
  • del.icio.us
  • Technorati
  • StumbleUpon
  • Google Bookmarks
  • Twitter
  • Facebook
  • FriendFeed
  • MySpace
  • Yahoo! Buzz
  • LinkedIn
  • Netvibes
  • RSS
Related posts:
  1. Template Tag Shortcodes plugin adds lots of flexbility to WordPress
  2. Brilliant new WordPress theme
  3. Which WordPress plugins should I donate to?
  4. WordPress theme: Paper Trail
  5. Is anyone still using WordPress 2.3 or earlier?

21 Comments

  1. very usefull, thanks

    Comment by Ali — December 27, 2009 @ 4:41 am

  2. This was very helpful, thanks. This exact solution didn’t work for me because I needed the date in another format, so I found this to work:

    Thought it might help those running into the same issue I did.

    Comment by Dax Davis — December 29, 2009 @ 7:20 am

  3. Good point but too techie. You don’t say exactly how to do it for us somewhat technically challenged.

    Comment by David — January 9, 2010 @ 11:57 pm

  4. David, this post is aimed at WordPress enthusiasts with some knowledge of theme editing. Perhaps I should have included some sort of disclaimer :) . The code snippet goes anywhere inside the_loop.

    Comment by Roy — January 10, 2010 @ 10:36 am

  5. ha, easy does it. cheers mayn.

    Comment by michiel — January 22, 2010 @ 10:46 am

  6. nicely done!

    Comment by rudy — January 26, 2010 @ 12:40 am

  7. Exactly what i needed! thanks :)

    Comment by Asko — January 30, 2010 @ 10:25 pm

  8. Works great! Thanks a lot.

    Comment by AJ Clarke — February 3, 2010 @ 2:06 am

  9. Man you saved my life :) . Tnx 4 sharing

    Comment by Igor — February 4, 2010 @ 5:21 pm

  10. Great Post!! Would love to add for those who would like to display the post date in WP sidebar

    3,
    ‘caller_get_posts’=>1
    );
    $my_query = new WP_Query($args);
    ( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <a href="” rel=”bookmark” title=”Permanent Link to “>

    Have a Great Day!

    Comment by Bobby — February 26, 2010 @ 4:15 am

  11. Link: Kako v Wordpressu prikažemo datum pri vsakem postu | Pomagalnik
  12. Looking for some way to remove the date from my static pages, i.e, home, about, etc. I am using the default classic theme. Any suggestions?

    Comment by Frank — March 13, 2010 @ 6:16 pm

  13. Hi Frank. You’ll need to edit the theme somewhat, but it’s not very hard. You can either create a copy of index.php, call it page.php and remove the line where the_date() is called, or wrap that same line in an if-statement and use is_page() or is_single(). I realize this isn’t exactly a full tutorial, but Google will help you :) .

    Comment by Roy — March 16, 2010 @ 4:46 pm

  14. thanks a lot, just what i was looking for!

    Comment by Lea — March 19, 2010 @ 2:37 pm

  15. Very useful. You saved me a lot of time. Thank you very much.

    Comment by 5-squared — March 23, 2010 @ 6:39 pm

  16. Thanks Roy, really saved me some head scratching. I figured it was happening because of multiple posts on the same day but didn’t know what to do about it!

    Comment by Peter Hobley — April 2, 2010 @ 9:03 pm

  17. Thanks~! Very useful.

    Comment by cooper — May 7, 2010 @ 9:14 am

  18. Perfect, Thanks.

    Comment by T — May 22, 2010 @ 3:37 am

  19. I had no idea why that was happening. Thanks a lot of point out why it happened and also the fix. Your the best!

    Comment by Jason — July 7, 2010 @ 12:51 am

  20. Yeeeeeha great!!! I think I love you (was a joke)

    Comment by Nillus — July 16, 2010 @ 2:19 pm

  21. Link: Start The Loops » [object HTMLImageElement]

Leave a comment

ss_blog_claim=d470d8430338c3ea2e4510a1b5577fa0