Optimize time display of articles and comments in WordPress

Source: Internet
Author: User
This article mainly introduces how to optimize the time display of articles and comments in WordPress. it describes how to set the time to display the specific time before and after the xxx time in relative time, you can refer to the comments posted on "XXX minutes before" and those posted on "XXX minutes before" on many blogs to show the time for comments, the improved time display method not only intuitively tells readers how long this article or comment has been posted, but also enhances the time of comment reply, because there were too many things in my hand a while ago, and I was unable to access the Internet during the working day, the style and function of the theme took a long time to write, recently, it was just the turn to make comments, so I gradually changed my comments style and functions by referring to the popular online style 1.1 points.

So .....
Go .....
Sacks ..... The comment date and the article date call function are different. the following uses the comment date as an example. Please adjust the article date by yourself.

Principle of improved time display mode
It is easy to use a built-in function of WordPress to process the time difference between the current time and the time when the articles and comments are published. the display time is X minutes, X hours, and X days.
This function is human_time_diff ()

Usage:

 <?php human_time_diff( $from, $to ) ;?>

Note:
Determine the difference between the two time tags.
Returns the time difference between the $ from and $ to time variables in readable formats, such as "1 hour", "5 Minutes", and "two days.

It is also easy to understand in English: from. (This sentence is useless, haha .)

Improved implementation of prototype

// Change the function displayed in your comment time to the following: <? Php echo human_time_diff (get_comment_time ('u'), current_time ('timestamp');?>

The time difference is calculated for all dates. is it violent?

Implementation of the primary edition
Simply add a judgment. if the comment time does not exceed one day, it is displayed XX hours ago. if the comment time exceeds one day, the original date is displayed.
Is this more user-friendly? The readers cannot always stick their fingers on what day is before 38 days? Haha!
Code:

<? Php // calculate whether the value exceeds one day. note: 86400 is the total number of seconds in a day. 60 seconds, 60 Minutes, X24 hours, = 86400 seconds. // if the value is not enough for a day, enter the value in your own calculation. If (current_time ('timestamp')-get_comment_time ('u') <86400) // Display items within one day {$ cmt_time = human_time_diff (get_comment_time ('u '), current_time ('timestamp ')). '-ago';} // else {$ cmt_time = get_comment_date ('Y/n/J') is displayed for more than one day '). '-'. get_comment_time ('','', false) ;};??> // Change the function displayed in your comment time to the following: <? Php echo $ cmt_time;?>

Enhanced Edition
So can we enhance it?
Why?
Well, because I am a real person, I feel that the Chinese display date is poor, and it affects my layout. I like to display dates in English, however, the Chinese version of WordPress in Chinese is really not dead-handed (the Chinese version is really careful). if we use the human_time_diff function to output it directly, the Chinese version of WordPress will display all the results in Chinese format XX Hours XX days ago, which may affect our layout, and there is no hook in this human_time_diff function, there is no reserved non-Chinese parameter, so there are only two ways to display English:

Directly modify the human_time_diff function to make the Chinese language ineffective. this is too violent and will be further modified after the upgrade. lunjia does not like it.
Rewrite a human_time_diff function to bypass localization.
The following code is strongly inserted into function. php:

// The day hour min of the original function is in lower case. // I change the first letter of the three words to uppercase, that is, Day Hour Min can be avoided in Chinese, you know? If (! Function_exists ('xz _ time_diff '): function xz_time_diff ($ from, $ to = '') {if (empty ($ to) $ to = time (); $ diff = (int) abs ($ to-$ from); if ($ diff <= 3600) {$ mins = round ($ diff/60 ); if ($ mins <= 1) {$ mins = 1 ;}/ * translators: min = minute */$ since = sprintf (_ n ('% s ', '% s mins', $ Mins), $ mins);} else if ($ diff <= 86400) & ($ diff> 3600 )) {$ hours = round ($ diff/3600); if ($ hours <= 1) {$ hours = 1 ;} $ since = sprintf (_ n ('% s hour',' % s hours', $ Hours), $ hours);} elseif ($ diff> = 86400) {$ days = round ($ diff/86400); if ($ days <= 1) {$ days = 1 ;} $ since = sprintf (_ n ('% s Day',' % s Days ', $ days), $ days);} return $ since;} endif;

The time judgment code is changed to the following:

<? Php // changes the name of the function that calculates the date difference. If (current_time ('timestamp')-get_comment_time ('u') <86400) {$ cmt_time = xz_time_diff (get_comment_time ('u'), current_time ('timestamp ')). '-ago';} else {$ cmt_time = get_comment_date ('Y/n/J '). '-'. get_comment_time ('','', false) ;};??> // Change the function displayed in your comment time to the following: <? Php echo $ cmt_time;?>

Show relative time of comments and articles

Based on the above version and the following version, it should be regarded as an enhanced community version. to achieve the effect, you still need to add code in the topic, so it hasn't arrived at the community version yet, haha.
The function code is as follows:

Relative time functions

If (! Function_exists ('xz _ time'):/*** encapsulate the function for displaying the relative time of the article and comment. * author: XiangZi http://PangBu.com/ * @ Param $ type string 'cmt' or 'art', used to define whether the comment time or article time is displayed. * @ Param $ ag_time numeric type is used to define the display relative time limit. the default value is 86400 seconds, that is, one day. * @ Param $ after: The text displayed after the relative time. the default value is '-ago' * @ param $ late, the default value is get_the_time ('Y/n/j-H: I ') or get_comment_time ('Y/n/j-H: I ') * @ return returns a string (relative time or absolute time) */function xz_time ($ type = 'art', $ aga_time = 86400, $ after = '-ago ', $ late = '') {if ($ type = 'cmt') {$ diff = (int) abs (get_comment_time ('u ') -current_time ('timestamp'); if ((! $ Late) | $ late = '') {$ late = get_comment_time ('Y/n/j-H: I ');};} if ($ type = 'art') {$ diff = (int) abs (get_the_time ('u')-current_time ('timestamp'); if ((! $ Late) | $ late = '') {$ late = get_the_time ('Y/n/j-H: I ');};} if ($ diff <= 3600) {$ mins = round ($ diff/60); if ($ mins <= 1) {$ mins = 1;}/* translators: min = minute */$ since = sprintf (_ n ('% s min',' % s mins', $ Mins), $ mins );} else if ($ diff <= 86400) & ($ diff> 3600) {$ hours = round ($ diff/3600); if ($ hours <= 1) {$ hours = 1 ;}$ since = sprintf (_ n ('% s hour',' % s hours', $ Hours), $ hours );} Elseif ($ diff> = 86400) {$ days = round ($ diff/86400); if ($ days <= 1) {$ days = 1 ;} $ since = sprintf (_ n ('% s Day',' % s Days ', $ days), $ days) ;}; $ since. = $ after; return $ diff <$ ag_time? $ Since: $ late;} endif;

Usage
Insert the above code into the function. php file of your topic.
Then you can call this function where you want to display the relative time.
At least one parameter is set for the function input, that is, the $ type string 'cmt' (comment time) or 'art' (Article time)
Example:

// The simplest way to call echo xz_time ('cmt'); // The output result in one day: 3 Hours-ago // The output result after one day: // The Call duration is relative time within two days. the default time echo xz_time ('cmt', 172800) is displayed before. // The output result within two days is as follows: 3 Hours-ago // The output result after 2 days:-// the relative time of the call within 2 days, the 'previous comments 'echo xz_time ('cmt', 172800, 'Previous comments') are displayed after the relative time; // The output results within 2 days: 3 Hours previous comments // output result after 2 days:-// the relative time of the call duration within 2 days, the previous time is displayed as year-month-day echo xz_time ('cmt', 172800, 'Previous comments', get_comment_time ('Y-n-J ')); // output results within 2 days: comments before 3 Hours // output results after 2 days:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.