如何在WordPress中显示相对日期

Do you want to display relative dates on your WordPress site? Relative dates are used on many popular social media websites like Twitter and Facebook. Instead of showing the date timestamps, these websites show how long ago something was posted. In this article, we will show you how to display relative dates in WordPress.

您想在WordPress网站上显示相对日期吗? 相对日期用于Twitter和Facebook等许多流行的社交媒体网站上。 这些网站没有显示日期时间戳,而是显示多久以前发布的内容。 在本文中,我们将向您展示如何在WordPress中显示相对日期。

How to display relative dates in WordPress
为什么以及何时在WordPress中使用相对日期? (Why and When to Use Relative Dates in WordPress?)

As you may have noticed that a lot of social networking websites use relative time to describe how long ago an entry was posted.

您可能已经注意到,许多社交网站都使用相对时间来描述条目发布的时间。

For example, two hours ago, yesterday, just now, etc.

例如,两个小时前,昨天,现在等等。

Relative date shown on Facebook

Relative dates give users a sense of how much time has passed since something was posted. This is why a lot of WordPress blogs and news sites are adding time ago style relative dates.

相对日期使用户了解自发布某项内容以来已经过了多少时间。 这就是为什么许多WordPress博客和新闻网站都添加时间前样式相对日期的原因。

Having said that, let’s take a look at how to add relative dates to your WordPress site.

话虽如此,让我们看一下如何在WordPress网站中添加相对日期。

使用插件在WordPress中添加相对日期 (Adding Relative Dates in WordPress Using a Plugin)

First, thing you need to do is install and activate the Meks Time Ago plugin. For more details, see our step by step guide on how to install a WordPress plugin.

首先,您需要做的是安装并激活Meks Time Ago插件。 有关更多详细信息,请参阅有关如何安装WordPress插件的分步指南。

Upon activation, you need to visit Settings » General page and scroll down to ‘Meks Time Ago Options’ section.

激活后,您需要访问“设置”»“常规”页面,然后向下滚动到“ Meks Time Ago Options”。

Meks Time Ago settings

You can select where and when you want to use relative time. You can choose to show it for date, time. or both. You can also limit it to posts not older than a specific time.

您可以选择要使用相对时间的时间和地点。 您可以选择显示日期,时间。 或两者。 您还可以将其限制为不超过特定时间的帖子。

If you run a multilingual site or want to change the text ‘ago’, then you can do that here as well.

如果您运行一个多语言站点或想要更改文本“ ago”,那么也可以在此处进行操作。

Don’t forget to click on the save changes button to store your settings.

不要忘记单击“保存更改”按钮来存储您的设置。

That’s all, you can now visit your website to see it in action on your posts and comments.

就是这样,您现在可以访问您的网站,以查看其在帖子和评论中的作用。

Relative date shown with a WordPress post
在WordPress中手动添加相对日期 (Adding Relative Dates in WordPress Manually)

This method requires you to install a plugin, but you will also have to add code manually to your WordPress theme.

此方法需要您安装插件,但还必须手动将代码添加到WordPress主题。

If this is your first time adding code to your WordPress site, then you may want to see our guide on pasting snippets from web into WordPress.

如果这是您第一次向WordPress网站添加代码,那么您可能需要查看有关将网页摘要粘贴到WordPress中的指南

You may also need to see how to use FTP to edit and upload files to WordPress.

您可能还需要了解如何使用FTP编辑文件并将其上传到WordPress

Ready? Let’s get started.

准备? 让我们开始吧。

First thing you need to do is install and activate the WP RelativeDate plugin.

您需要做的第一件事是安装并激活WP RelativeDate插件。

Upon activation, the plugin will try to replace the default WordPress dates in your theme with relative date and time.

激活后,插件将尝试用相对日期和时间替换主题中的默认WordPress日期。

However, since there are so many WordPress themes using different methods to display date and time, it may not be able to work with your theme.

但是,由于有太多WordPress主题使用不同的方法来显示日期和时间,因此它可能无法与您的主题配合使用。

Visit your website to see if it was able to successfully change date and time to relative dates.

访问您的网站以查看它是否能够成功将日期和时间更改为相对日期。

If it does not work for your theme, then you may need to edit your WordPress theme files where you want to show relative date and time.

如果它不适用于您的主题,那么您可能需要在要显示相对日期和时间的地方编辑WordPress主题文件。

For example, if you want to show relative date on a single post page, then you will need to edit single.php or content-single.php files. For comments, you may need to edit comments.php file.

例如,如果要在单个帖子页面上显示相对日期,则需要编辑single.php或content-single.php文件。 对于注释,您可能需要编辑comment.php文件。

Basically, you will be looking for any of these lines in your theme’s template files:

基本上,您将在主题的模板文件中寻找以下任何行:


<?php the_date(); ?>
<?php get_the_time(); ?>
<?php get_the_date(); ?>

You will need to replace them with the following line:

您将需要用以下行替换它们:

<?php relative_post_the_date(); ?>

You may need to edit multiple files in your WordPress theme depending on where you want to display relative dates on your site.

您可能需要在WordPress主题中编辑多个文件,具体取决于您希望在网站上显示相对日期的位置。

That’s all, you can now visit your website to see relative dates in action.

就是这样,您现在可以访问您的网站以查看相对日期。

We hope this article helped you learn how to display relative dates in WordPress. You may also want to see our list of 25 most useful WordPress widgets for your site.

我们希望本文能帮助您学习如何在WordPress中显示相对日期。 您可能还想查看我们的网站25个最有用的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视频教程。 您也可以在TwitterFacebook上找到我们。

翻译自: https://www.wpbeginner.com/plugins/how-to-display-relative-dates-in-wordpress/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值