在 functions.php 文件中加入下段代码,然后在需要使用的位置添加<?php the_time(); ?>调用即可
function Bing_filter_time() { global $post ; $to = time(); $from = get_the_time('U') ; $diff = (int) abs($to - $from); if ($diff <= 3600) { $mins = round($diff / 60); if ($mins <= 1) { $mins = 1; } $time = sprintf(_n('%s分钟', '%s分钟', $mins), $mins) . __('前' , 'Bing'); } else if (($diff <= 86400) && ($diff > 3600)) { $hours = round($diff / 3600); if ($hours <= 1) { $hours = 1; } $time = sprintf(_n('%s小时', '%s小时', $hours), $hours) . __('前' , 'Bing'); } elseif ($diff >= 86400) { $days = round($diff / 86400); if ($days <= 1) { $days = 1; $time = sprintf(_n('%s天', '%s天', $days), $days) . __('前' , 'Bing'); } elseif ($days > 29) { $time = get_the_time(get_option('date_format')); } else { $time = sprintf(_n('%s天', '%s天', $days), $days) . __('前' , 'Bing'); } } return $time; } add_filter('the_time','Bing_filter_time');
改完之后可能会遇到明明是刚发布的却显示时间为8小时之前,这是因为WordPress使用的时区和我们本地的时差,把 wp-includes\functions.php文件中的
function current_time( $type, $gmt = 0 )
改成
function current_time( $type, $gmt = 8 )
即可
评论