Display last modified date on a Wordpress site

2019-08-29 23:34发布

问题:

I was wondering if anyone knows an effective way of displaying the last modified date on a Wordpress site.

I'm not interested in the last modified date of a single post or page, but in a date (and maybe time) that shows the last time ANY modification was made on a site.

For example:

  • I modify the "Hello World" post on 15/09/18
  • I modify the "Sample page" page on 16/09/18
  • The "My last modified" displays "16/09/18" on the "Hello World" post and also on the "Sample page" page.

I'm aware of the the_modified_date() and the get_the_modified_date() functions, but I'd like to effectively show the latest from all of the modified content (pages, posts, custom post types, etc.)

Thank you for your suggestions in advance!

回答1:

You could query the most recently updated posts and pages, then pull out the updated date from there. Try something like this.

$recently_updated_posts = new WP_Query(array(
    'post_type'      => array('any'),
    'posts_per_page' => 1
    'orderby'        => 'modified',
    'no_found_rows'  => true, // speed up query when we don't need pagination
));

Then, you can use the $recently_updated_posts in the standard WordPress loop and have access to the_modified_date() and the get_the_modified_date() functions.