某些站长更新比较勤快,那么就可以选择更友好的发表时间显示方式。一般情况下我们可以看到发布新微博都会显示成1分钟前、1个小时前、多久前、几分钟前、几天前等字样,这样的显示对访客、用户来说很人性化,体验也很好,感觉是这个站点一直很活跃,所以WordPress站点也应该有这样的实用技巧。
自定义函数-加入functions.php
function timeago( $ptime ) {
$ptime = strtotime($ptime);
$etime = time() - $ptime;
if ($etime < 1) return '刚刚';
$interval = array (
12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')',
30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $ptime).')',
7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')',
24 * 60 * 60 => '天前',
60 * 60 => '小时前',
60 => '分钟前',
1 => '秒前'
);
foreach ($interval as $secs => $str) {
$d = $etime / $secs;
if ($d >= 1) {
$r = round($d);
return $r . $str;
}
};
}
列表页和文章页面使用方法
适用于 index.php | search.php | tag.php | single.php | page.php | author.php | category.php
在需要显示时间的地方替换成以下,注意需要放在?php代码块中:
echo '发表于 '.timeago( get_gmt_from_date(get_the_time('Y-m-d G:i:s')) );
评论区域使用方法
在需要显示时间的地方替换成以下,注意需要放在评论循环内:
echo '发表于 '.timeago( $comment->comment_date_gmt );
注意
这个版本使用时不需要划定时区
<?php date_default_timezone_set('PRC');/*东八区时间调整*/?>
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/4972.html
