WordPress设置文章发布时间为多久之前!

WordPress一天一个小技巧!

Published in Feb-15-2022

在 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 )

即可



评论

曹同学的bilibili 曹同学的知乎 曹同学的email
返回网站顶部
正在获取,请稍候...
00:00/00:00