如何讓 WordPress 時間顯示為「幾天前」?

對一般人來說,顯示文章的相對時間可能更容易閱讀,例如「幾小時前」或「幾天前」,而眾所皆知的 Facebook 就以類似方式來顯示時間。你也想在 WordPress 使用此方式來顯示時間嗎?那麼透過 WordPress 的 human_time_diff() 函式就能讓時間格式變成「5天前」、「16小時前」。

將以下原始碼複製貼上於文章適當位置(必須在loop裡):

發佈於 <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . '前'; ?>

如果要使用於迴響的時間,可以使用以下原始碼:

發佈於 <?php echo human_time_diff(get_comment_time('U'), current_time('timestamp')) . '前'; ?>

這樣就完成囉!

This entry was posted in WordPress技巧. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

發表迴響

您的電子郵件位址並不會被公開。 必要欄位標記為 *

*

您可以使用這些 HTML 標籤與屬性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>