Do you want to display last updated date for your posts in WordPress? Some websites regularly update their posts and would like to show users when the article was last updated. In this article, we will show you how to easily display the last updated date of your posts in WordPress.
您是否要在WordPress中显示帖子的最后更新日期? 一些网站会定期更新其帖子,并希望向用户展示文章的最新更新时间。 在本文中,我们将向您展示如何轻松地在WordPress中显示帖子的最后更新日期。
![How to display last updated date of your posts in WordPress How to display last updated date of your posts in WordPress](https://i-blog.csdnimg.cn/blog_migrate/f9b34a012f0eed8703da5b426d202828.png)
何时需要WordPress中帖子的上次更新日期? (When You Need Last Updated Date for Posts in WordPress?)
Most WordPress themes usually show the date when a post was last published. This is fine for most blogs and static websites.
大多数WordPress主题通常会显示帖子的最后发布日期。 这对于大多数博客和静态网站都很好。
However, WordPress is also used by websites where old articles are regularly updated (like ours). This last updated date and time is important information for those publications.
但是,定期更新旧文章的网站(例如我们的网站)也使用WordPress。 最后更新的日期和时间是这些出版物的重要信息。
The most common example is news websites. They often update old stories to show new developments, add corrections, or media files. If they only added the published date, then their users would miss those updates.
最常见的示例是新闻网站。 他们经常更新旧的故事以显示新的进展,添加更正或媒体文件。 如果他们仅添加发布日期,则其用户将错过这些更新。
Many popular blogs and websites don’t show any date on their articles. This is a bad practice and you should never remove dates from your blog posts.
许多流行的博客和网站都没有在文章中显示日期。 这是一种不好的做法, 您绝对不应从博客文章中删除日期。
Having said that, let’s see how to easily display last updated date for your posts in WordPress.
话虽如此,让我们看看如何在WordPress中轻松显示帖子的最新更新日期。
影片教学 (Video Tutorial)
If you don’t like the video or need more instructions, then continue reading.
如果您不喜欢该视频或需要更多说明,请继续阅读。
在WordPress中显示上次更新日期 (Displaying Last Updated Date in WordPress)
This tutorial requires you to add code to your WordPress files. If you haven’t done this before, then we recommend you to look at our guide on how to copy paste code in WordPress.
本教程要求您将代码添加到WordPress文件中。 如果您以前没有做过,那么我们建议您查看有关如何在WordPress中复制粘贴代码的指南。
Method 1: Show Last Updated Date Before Post Content
方法1:在发布内容之前显示最后更新的日期
You will need to add this code to your theme’s functions.php file or a site-specific plugin.
您将需要将此代码添加到主题的functions.php文件或特定于站点的插件中 。
function wpb_last_updated_date( $content ) {
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
$updated_date = get_the_modified_time('F jS, Y');
$updated_time = get_the_modified_time('h:i a');
$custom_content .= '<p class="last-updated">Last updated on '. $updated_date . ' at '. $updated_time .'</p>';
}
$custom_content .= $content;
return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );
This code checks to see if a post’s published date and last modified dates are different. If they are, then it displays last modified date before the post content.
此代码检查帖子的发布日期和上次修改日期是否不同。 如果是,则显示帖子内容之前的最后修改日期。
You can add custom CSS to style the appearance of the last updated date. Here is a little CSS that you can use as starting point:
您可以添加自定义CSS来设置上次更新日期的外观样式。 这里有一些CSS可以用作起点:
.last-updated {
font-size: small;
text-transform: uppercase;
background-color: #fffdd4;
}
This is how it looked on our demo website.
这就是我们的演示网站上的样子。
![Last updated date in post content Last updated date in post content](https://i-blog.csdnimg.cn/blog_migrate/19bd44a0feea6bd710a3bc32a97801e0.png)
Method 2: Add Last Updated Date in Theme Templates
方法2:在主题模板中添加上次更新日期
This method requires you to edit specific WordPress theme files. Many WordPress themes now use their own template tags which define how these themes show post meta data like date and time.
此方法要求您编辑特定的WordPress主题文件。 现在,许多WordPress主题使用它们自己的模板标签,这些模板标签定义这些主题如何显示日期和时间等元数据。
Some themes also use content templates or template parts to display posts.
一些主题还使用内容模板或模板部分来显示帖子。
Few simpler themes will use single.php, archive.php, and other template files to show content and meta information.
很少有更简单的主题会使用single.php,archive.php和其他模板文件来显示内容和元信息。
You will be looking for the code responsible for displaying the date and time. You can then either replace that code with the following code, or add it right after your theme’s date and time code.
您将寻找负责显示日期和时间的代码。 然后,您可以用以下代码替换该代码,或在主题的日期和时间代码后立即添加。
$u_time = get_the_time('U');
$u_modified_time = get_the_modified_time('U');
if ($u_modified_time >= $u_time + 86400) {
echo "<p>Last modified on ";
the_modified_time('F jS, Y');
echo " at ";
the_modified_time();
echo "</p> "; }
This is how it looked on our demo site:
这是在我们的演示站点上的样子:
![Last updated date in post meta Last updated date in post meta](https://i-blog.csdnimg.cn/blog_migrate/d0373681326e0c599dacebcb492a9318.png)
We hope this article helped you learn how to display last updated date of your posts in WordPress. You may also want to see our list of most useful time saving WordPress shortcuts.
我们希望本文能帮助您学习如何在WordPress中显示帖子的最后更新日期。 您可能还想查看我们最节省时间的WordPress快捷方式列表。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
如果您喜欢这篇文章,请订阅我们的YouTube频道 WordPress视频教程。 您也可以在Twitter和Facebook上找到我们。
翻译自: https://www.wpbeginner.com/wp-tutorials/display-the-last-updated-date-of-your-posts-in-wordpress/