Optimize the time display of articles and comments in WordPress, WordPress review _php Tutorials

Source: Internet
Author: User

Optimize the time display of articles and comments in WordPress, WordPress comment


Many blogs like to use comments published in "XXX minutes ago", the article published in "XXX minutes ago" to display the time of the article comments, improve the time display method not only can be very intuitive to tell readers this article or comment published how long it has been, but also to enhance the time sense of comment reply, elder brother likes, Because before a while in the hands of things too much, working days and suffering from the internet, so the theme of the style and function part of the 1.1-point write dragged for a long time, recently just turn to toss the review, so gradually refer to the online popular style of 1.1 to revise their own comments style and function.

So .....
Go .....
Dealing with sacks ... the comment date and the article date call function are different, the following is the review date as an example, the article date please adjust yourself.

The principle of improved time display mode
Very simple, is through a built-in function of WordPress to deal with the current time and the article, comment on the difference between the publication, the display is now x minutes, x hours, x days.
This function is Human_time_diff ()

Usage:

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

Description
Determine the difference between two time markers.
In human readable format, such as "1 hours", "5 minutes", "Two Days", returns the time difference between $from and $to two variables.

It is also very well understood in English: from the To. (This sentence is very useless, haha.) )

The prototype version improves the implementation

Change the function of your comment time display to the following <?php Echo Human_time_diff (get_comment_time (' U '), Current_time (' timestamp ')); >

All dates are calculated time difference, very violent, right?

Implementation of the primary version
Simply add a judgment if the comment time does not exceed one day before the XX hour is displayed, if more than one day displays the original date.
This is more humane, right? It's not always possible for readers to count their fingers 38 days ago. Ha ha!
Code:

<?php//Calculate whether more than one day note: 86400 is the total number of seconds in a day 60 seconds X60 minutes X24 hours =86400 seconds//If you feel that the day is not enough, please do your own calculation fill in. if (current_time (' timestamp ')-get_comment_time (' U ') < 86400)//What is displayed within a day {$CMT _time = Human_time_diff (get_comment_ Time (' U '), Current_time (' timestamp ')). '-ago ';} More than a day so show else{$cmt _time = get_comment_date (' y/n/j '). '-'. Get_comment_time (', ', false);}; ;? >//Change the function of your comment time display to the following <?php echo $CMT _time; >

Enhanced version
So, can we just get a little bit stronger?
Why enhance?
Well, because I this person more serious, think Chinese display date is not good-looking, affect my typesetting, like English display date, and the Chinese version of the WordPress Han really is no dead corner (Han of really careful), if we directly with the Human_time_diff function output, the Chinese version of the WordPress will be the result of all Chinese display xx hours xx days ago, this is likely to affect our typesetting, and this human_time_diff function has neither left the hook, nor reserved a non-Chinese parameters, so we want to display English words, there are only two ways:

Directly modify the Human_time_diff function, so that the Chinese to fail, so too violent, and later upgrade also to go in to change, LUN home do not like.
Rewrite one of your own Human_time_diff functions, bypassing Chinese.
Inside the function.php, insert the following code strongly:

The original function of day hour min are lowercase,//I changed the first letter of the three words to uppercase, that is, Day hour min can avoid the Han, you understand? if (! function_exists (' Xz_time_diff ')): function Xz_time_diff ($from, $to = ") {if (empty ($to)) $to = time (); $di FF = (int) abs ($to-$from); if ($diff <= 3600) {$mins = round ($diff/60), if ($mins <= 1) {  $mins = 1;}/* Translators:min=minute */$si nce = sprintf (_n ('%s Min ', '%s Mins ', $mins), $mins); } else if (($diff <= 86400) && ($diff > 3600)) {$hours = round ($diff/3600); if ($hours <= 1) {  $ho URS = 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 should read as follows:

<?php//Just change the name of the function that calculates the date difference, the other ibid. if (current_time (' timestamp ')-Get_comment_time (' u ') < 86400) {$CMT _time = Xz_time_diff (Get_comment_time (' U '), Curr Ent_time (' timestamp ')). '-ago ';} else{$CMT _time = get_comment_date (' y/n/j '). '-'. Get_comment_time (', ', ' false ');}; ;? >//Change the function of your comment time display to the following <?php echo $CMT _time; >

Show comments, article relative time

According to the above version, the following should be considered to enhance the improved version of it, because to achieve the effect or need to add code in the theme, so not to the ultimate version, haha.
The function code is as follows:

Relative Time function

if (! function_exists (' Xz_time ')):/** * Displays the wrapper function for the article, commenting on the relative time. * Author: Xiangzi http://PangBu.com/* @param $type type string ' CMT ' or ' art ', used to define whether the comment time or article time is displayed. * @param $ago _time Numeric type defines the time limit for displaying relative time by default to 86,400 seconds, or one day. * @param $after String type text that appears after the relative time, default to '-ago ' @param $late items that are displayed after the string type exceeds the time limit, default to Get_the_time (' y/n/j-h:i ') or get_commen  T_time (' y/n/j-h:i ') * @return return string (relative time or absolute time) */function xz_time ($type = ' Art ', $ago _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 < $ago _time? $since: $late;} endif

How to use
Insert the above code into your topic's function.php file
Then call the function where you want to display the relative time.
function minimum input set a parameter that is $type type string ' CMT ' (Comment time) or ' art ' (article time)
Example:

The simplest call to echo xz_time (' CMT ');//day output: 3 Hours-ago///day output: 2015/12/26-20:01  //Call duration is 2 days relative to time, before time display default time Echo Xz_time (' CMT ', 172800); output results in//2 days: 3 Hours-ago//2 days after output: 2015/12/26-20:01  //Call duration is 2 days relative time, relative time after display ' previous comment ' echo Xz_time (' CMT ', 172800, ' previous comments '); output results in//2 days: 3 Hours comments//2 days later output: 2015/12/26-20:01//Call duration is 2 days relative to time, before time shown as Year-month-day Echo xz_time (' CMT ', 172800, ' previous comments ', Get_comment_time (' y-n-j ')); output from//2 days: 3 Hours ago Comments//2 days after output: 2015/12/26

Articles you may be interested in:

    • Example of implementation of review submission using AJAX technology in WordPress
    • The method of using Ajax asynchronously to get comments on user's avatar in WordPress
    • PHP functions for invoking comment templates and looping out comments in WordPress
    • WordPress restricts non-admin users to comment only once after the article
    • Explain the PHP functions used to get Comment templates and search forms in WordPress
    • Troubleshoot WordPress using CDN Post cannot comment on the error
    • Using jquery to realize the ID hover of the @ in WordPress to display the comment content
    • Write PHP script to realize the function of commenting paging in WordPress
    • Example of modifying a PHP script to make WordPress block spam comments
    • Customize default and lazy load for comments avatar in WordPress
    • Some optimization methods of guest comment function in WordPress
    • Implementation of mouse hover display and hide Comment and reference button in WordPress

http://www.bkjia.com/PHPjc/1091101.html www.bkjia.com true http://www.bkjia.com/PHPjc/1091101.html techarticle optimize WordPress in the article and comment on the time display, WordPress review Many blogs like to use comments published in "XXX minutes ago", the article published in "XXX minutes ago" to show ...

  • Related Article

    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.