in Tutorials

How to display updated publication date in WordPress themes

I was recently asked how you can display the time a story or post was updated in a WordPress theme, like what you see when major outlets cover a breaking and rapidly evolving news story. Suggestions I’ve heard before—and quite often, the instant reaction of WordPress users—is to look for a plugin or utilize custom fields.

But neither method is the best way to do it. Using core WordPress functions and template tags, you can change any of your theme’s templates to display when a post was last updated or the “time ago” date, which has become popular through the real-time web and services like Facebook and Twitter.

Updated date and time

Seattle Times Updated Publication Dates

Examples of updated publication dates in use on The Seattle Times website.

The core WordPress function for displaying the last time a post or page was updated is the_modified_time() (or the_modified_date() for that matter). To show when your post was last updated, you simply need to insert this code inside The Loop of your theme wherever you would like it:

<?php the_modified_time('g:i a'); ?>

Just add a little bit of text before it, like “Updated” and style it, and you’ve got the last time a post was updated displaying in “Updated Hour:Minute am/pm” format. Of course, you can modify this in all sorts of ways with the function reference as a guide.

Human time distance or “time ago”

You can just as simply display how long ago it was that a post or comment was published with the human_time_diff() WordPress function, as seen on The New York Times’ website in breaking coverage of the shooting of Rep. Gabrielle Giffords:

New York Times human reference dates and timesThis bit of code will display for users how long ago a post was published when dropped into your templates:

<?php echo human_time_diff(get_the_time('U'),
current_time('timestamp')) . ' ago'; ?>

Comments are closed.