I'm looking forward to display, somewhere in the homepage of a WP site, a legend saying:
Last updated: XX/XX
by grabbing the date from the most recent post, but not showing any of its content, just the date.
Is there any quick way to do this?
I would save an option on post save:
add_action(
'save_post',
function($id,$p) {
if (
(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
|| (defined('DOING_AJAX') && DOING_AJAX)
|| ($p->post_status === 'auto-draft')
) {
return;
}
update_option('_last_site_update',$p->post_date);
},
1,2
);
And retrieve it with a variant of the function provided by @G-M :
function my_last_updated( $format = '' ) {
$last = get_option('_last_site_update');
if ( empty($last) ) return;
if ( empty($format) ) {
$format = get_option('date_format');
}
return mysql2date($format,$last);
}
echo my_last_updated();
This way you:
wp_get_recent_posts
),get_option
query,There are different way to do this.
The easiest, in my opinion is to use wp_get_recent_posts
to retrieve the last post and the print the post modified date.
Wrapping it in a function make it flexible and reusable. In your functions.php
you can put:
function my_last_updated( $format = '' ) {
$lasts = wp_get_recent_posts( array('numberposts'=>1, 'post_status'=>'publish') );
if ( empty($lasts) ) return;
$last = array_pop($lasts);
if ( empty($format) ) $format = get_option('date_format');
return mysql2date( $format, $last['post_modified'] );
}
Then you can use it like so:
<p>Last updated: <?php echo my_last_updated() ?>.</p>
The argument $format
let you choose a different date format for the date see here to choose one. If nothing is passed, the format set in WP options is used.