WordPress 本身调用时间的函数 the_time() 只能直接调用时间,通过他的 filter,我们可以让他显示为比较科学的几天前格式。
将下边的代码丢到 function.php 的最后一个 ?> 前即可。
/**
* WordPress 修改时间的显示格式为几天前
* https://www.wpdaxue.com/time-ago.html
*/
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'); |
/**
* WordPress 修改时间的显示格式为几天前
* https://www.wpdaxue.com/time-ago.html
*/
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');
上边的代码可以让 30 天内发布的文章显示为几天前,而过了 30 天即显示为正常的标准格式日期。
来源:
https://www.wpdaxue.com/time-ago.html
微信公众号
手机浏览(小程序)
Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(): Failed to enable crypto in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57
Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_32730.jpg): failed to open stream: operation failed in
/mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line
57