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

posts - How to get the latest posting time of archived pages in WordPress?

programmeradmin0浏览0评论

Is there a way to get the post time of the latest post under the WordPress archive page?

I would like to add a feature to the archive page to show the latest time of posts published under that category.

Any help, thanks in advance!

Is there a way to get the post time of the latest post under the WordPress archive page?

I would like to add a feature to the archive page to show the latest time of posts published under that category.

Any help, thanks in advance!

Share Improve this question asked Dec 20, 2020 at 12:21 MatthewMatthew 1515 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Query the posts ordered by date, restricted to 1 post to display, then get the date.

$args = array(
   'posts_per_page' => 1,
   'orderby' => 'date',
   'order' => 'DESC'
);

$lastpost = get_posts($args);
echo $lastpost[0] -> post_date;

Edited last line for proper display:

$lastpostdate = $lastpost[0] -> post_date;
echo '<div class="lastpostdate>"' . $lastpostdate . '</div>';

Or, if you already have the query with the default order, you can just get the last post's date from it:


// assuming $myquery is your query

$lastpostdate =  $myquery -> posts[0] -> post_date;

Or, if you have not altered the global query, in archives just use:

$lastpostdate = $wp_query -> posts[0] -> post_date;
发布评论

评论列表(0)

  1. 暂无评论