Recently I was writing a post in a WordPress blog that I knew I would be updating periodically and wondered if there was any way I could get the most recent update date to display in the summary shown on the home page, or basically at the top of the post. After some searching and testing here’s what I came up with.
When you do a search for this sort of thing you’ll find several plugins available but many I found would display an updated date on every post. Some would allow you to select certain posts, but they seemed rather complex on the surface. I generally don’t like to add a plugin with the added overhead and possible security problems when a simple function will suffice. This insures my sites will load as quickly as possible.
There’s not a lot out there on creating a function to do this so I dug through the WordPress develop docs and consulted a few other references and was able to create this function.
// Shortcode :: Modified Date //
function c3moddate_shortcode($atts) {
global $post;
if (empty($atts['format'])) {
$atts['format'] = get_option("M j, Y g:i a");
}
$c3mod_date = 'Updated: ' . get_the_modified_date($atts['format'],$post->ID);
return $c3mod_date;
}
add_shortcode('c3moddate', 'c3moddate_shortcode');
This allows you to place a shortcode in a WordPress post at the spot where you would like the updated date to appear. Simple and selective. To use it simply place the following shortcode in your post.
[c3moddate format="M j, Y g:i a"]
The format is how PHP renders date and time. If you would like to see what the various options are look here. Otherwise you can use this as is and what you’ll see in your post will look like this (using your theme style settings of course).
Simple and selective for just a single post. Of course you can use it as often as you like. It’s lightweight and adds almost nil to your web site load time which is a very good thing.
Enjoy!
Leave a comment, or trackback from your own site.