最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Get the most recently modified post date of most recently modified post

programmeradmin2浏览0评论

I have Googled this, but there are always answers to get the modified date of a single post, which I know how to do.

What I'd like to do is get and display the most recent modified date of the most recently modified post.

In other words, if I have 100 posts on a site and someone modifies any one of them, I want to display that date elsewhere on the site.

As in "Most recent site update: March 25, 2018"

I have Googled this, but there are always answers to get the modified date of a single post, which I know how to do.

What I'd like to do is get and display the most recent modified date of the most recently modified post.

In other words, if I have 100 posts on a site and someone modifies any one of them, I want to display that date elsewhere on the site.

As in "Most recent site update: March 25, 2018"

Share Improve this question asked Mar 22, 2018 at 13:26 SteveSteve 3139 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try this:

$q = new WP_Query;

$posts = $q->query( array(
    'posts_per_page' => 1,
    'orderby'        => 'post_modified', // Sorts by the date modified.
    'order'          => 'DESC',          // Sorts in descending order.
    'no_found_rows'  => true,
) );

// Note that $posts could have more than one post, if your site have sticky
// posts. However, the first post, unless filtered by plugins, would always
// be the most recently modified/updated one.
if ( ! empty( $posts ) ) {
    $post = $posts[0];
    setup_postdata( $post );
    ?>
        <p>Most recent site update: <?php the_modified_date( 'F d, Y' ); ?>.</p>
    <?php
    wp_reset_postdata();
}
发布评论

评论列表(0)

  1. 暂无评论