如何让wordpress文章显示x秒x分x小时x天x月x年之前?
代码如下(放入function.php):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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'); |
原文链接:https://xiaohost.com/11020.html,转载请注明出处。
评论0