How to Display the Date for Multiple Posts in WordPress

Posted on: No Comments

File this one under things you wouldn’t expect: when displaying a list of posts in WordPress, if you try to display the date using the the_date() function and have multiple posts published on the same day, only the first post will display the date.

This makes sense when you think back to the origins of blogs as date-based journal entries. But in today’s use of blogging software to run websites where the content isn’t always categorized by date, this convention is less useful.

Enter two WordPress template tags: the_time() and get_the_date(). You can use either one to retrieve the date of the post and in both cases you’ll have a couple options on how to display the date.

One way is to tap into your blog options and retrieve the date format you’ve set in the General Settings for your site:

<?php the_time(get_option('date_format')); ?>

Or you can customize the format with a date-specific format string, for example using the_time():

<?php the_time('l, F j, Y'); ?>

Which would generate something like: Monday, January 1, 2010

Personally, I’d recommend this latter option as it gives you more granular control in case you ever need to format the date differently in different locations of your site.

Tags:
Posted on: No Comments