WordPress时间格式改为“XX天前”

Gavin Wu 2021年01月16日 9:01 Wordpress 1,189 Views

wordpress 默认的时间格式是具体时间,想要改成多少天前的样式。可以使用以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
function timeago() {
        global $post;
        $date = $post->post_date;
        $time = get_post_time('G', true, $post);
        $time_diff = time() - $time;
        if ( $time_diff > 0 && $time_diff < 24*60*60 )
            $display = sprintf( __('%s ago'), human_time_diff( $time ) );
        else
            $display = date(get_option('date_format'), strtotime($date) );
        return $display;
    }
    add_filter('the_time', 'timeago');

调用方法

1
<?php the_time();?>

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Top