admin管理员组

文章数量:1290946

I would like to be able to post the most recent updated date in my footer. However, I can't seem to find a way to do that globally for the site, just for each post/page at a time using get_the_modified_date().

So for example if I make a change to my home page the footer would be updated to that date and time. If I later make an edit to a miscellaneous page, then the "Updated" date and time would change to reflect that more recent update.

Any help is greatly appreciated.

I would like to be able to post the most recent updated date in my footer. However, I can't seem to find a way to do that globally for the site, just for each post/page at a time using get_the_modified_date().

So for example if I make a change to my home page the footer would be updated to that date and time. If I later make an edit to a miscellaneous page, then the "Updated" date and time would change to reflect that more recent update.

Any help is greatly appreciated.

Share Improve this question asked Jul 30, 2020 at 18:27 IisraelIisrael 1357 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Just a continuation from mozboz' answer. For those curious as I was, you could use the date() or date_i18n() functions to format the date properly in the following snippet.

function get_latest_update_date() {
    global $wpdb;
    $thelatest = $wpdb->get_var("SELECT max(post_modified) FROM wp_posts WHERE post_type IN ('post', 'page');");
    //returns formatted date like 13.08.2001
    return date_i18n('j.m.Y', strtotime($thelatest));
  }
  
  add_shortcode('latestupdatedate', 'get_latest_update_date');

I don't know if there's a more WP-friendly way to do this, but this query will do what you want. It gets the latest date from the posts table for any post or page:

select max(post_modified) from wp_posts where post_type IN ('post', 'page');

So you could wrap that up in a shortcode for example, like:

function get_latest_update_date() {
  global $wpdb;
  return $wpdb->get_var("SELECT max(post_modified) FROM wp_posts WHERE post_type IN ('post', 'page');");
}

add_shortcode('latestupdatedate', 'get_latest_update_date');

If you want to format the date, you'll need to add the PHP date parsing/formatting functions.

本文标签: templatesHow to get the most recent modified date of anything in the footer of my site