wordpress 如何让文章时间显示为多少天几周几月几年前?

作者: 站长 上传时间: 浏览: N/A 下载: N/A 格式: N/A 评分: N/A

如何让wordpress文章显示x秒x分x小时x天x月x年之前?

代码如下(放入function.php):

function format_date($time)
{
global $options, $post;
$p_id = isset($post->ID) ? $post->ID : 0;
$q_id = get_queried_object_id();
$single = $p_id == $q_id && is_single();
if (isset($options['time_format']) && $options['time_format'] == '0') {
return date(get_option('date_format') . ($single ? ' ' . get_option('time_format') : ''), $time);
}
$t = current_time('timestamp') - $time;
$f = array(
'86400' => '天',
'3600' => '小时',
'60' => '分钟',
'1' => '秒'
);
if ($t == 0) {
return '1秒前';
} else if ($t >= 604800 || $t < 0) { return date(get_option('date_format') . ($single ? ' ' . get_option('time_format') : ''), $time); } else { foreach ($f as $k => $v) {
if (0 != $c = floor($t / (int)$k)) {
return $c . $v . '前';
}
}
}
}
add_filter('the_time','format_date');

Leave a Comment