在WordPress中设置24小时内的文章日期格式为“之前”发表

WordPress主题中,常见的日期显示格式——“2010年5月20日”,本文将介绍一种新的日期显示格式——“20分钟之前发表”,这种日期显示格式很适合那些内容更新比较频繁的博客。

一. 先看一下 Google实时搜索 中的日期显示格式
在wordpress 中设置24小时内文章日期格式为“之前发表”

上面的日期显示格式效果很酷吧,在wordpress中只需要增加简单的几行代码也可以实现上述的效果。

二. 具体实现方法

只需要在主题文件夹的 functions.php 文件中增加下面的代码即可。

add_filter('the_time', 'timeago');

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 之前发表'), human_time_diff( $time ) );
else
$display = date(get_option('date_format'), strtotime($date) );
return $display;
}

三. 说明

sprintf( __(‘%s 之前发表’), human_time_diff( $time ) ) ——日期显示格式,可根据需要自行修改“ %s 之前发表 ”(不包含引号)即可。

发表评论

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

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:wink: :-| :-x :twisted: :) 8-O :( :roll: :-P :oops: :-o :mrgreen: :lol: :idea: :-D :evil: :cry: 8) :arrow: :-? :?: :!:

无觅相关文章插件,快速提升流量

任鸟飞网页设计博客 谜题推理 No.1 Web Design Gallery IndustryIdea LookForDesign AWebGallery Mobile Web Template iDesign Wallpaper Big Funny Picture iPhone Faves iPhone Mobi iPad Faves Android Faves BlackBerry Faves Best Design Magazine All Banner Templates Blog Earn Tips 是谁啊 只爱美人 家乡美 唯美爱 美人衣妆 AutoCAD 2D | Pro/E 3D江苏名企The HTML5 Templates