实现让WordPress显示时间为几天前

还是老规矩,在functions.php添加代码。

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');

如果在1小时内,会显示XX分钟前;如果在24小时内,会显示XX小时前;如果在7天内,会显示XX天前;如果超过了7天,则会显示默认的时间。

发表评论

加入官方QQ群

了解一下 了解一下