由于从事码代码的工作,已经习惯于晚上到茶话汇这里写文章,经常会出现在晚上23点XX分提交发布文章,但过了第二天很多读者来到这一看,发布时间是头一天的文章才这么点阅读量。。。
出于在乎那点阅读量的“虚荣”,也可以说是为了给读者更好的体验,所以就打算将文章列表里所看到的文章发布时间调整为“发布于XX小时”前,在网上查了下基于Wordpress实现的方法,现在在茶话汇已经用上了,大家也就别动不动就想着用插件,自己研究下不难的,在这里把代码分享给大家,有兴趣的可以参考下。可以简单修改参数“定制对多少小时内的文章”才按照该规则显示……
1. 首先将下面的代码写入到你的WP模版的functions.php文件中。
[code lang=”php”]function time_since(older_date,newer_date = false)
{
chunks = array(
array(60 * 60 * 24 * 365 , ‘年’),
array(60 * 60 * 24 * 30 , ‘月’),
array(60 * 60 * 24 * 7, ‘周’),
array(60 * 60 * 24 , ‘天’),
array(60 * 60 , ‘小时’),
array(60 , ‘分钟’),
);newer_date = (newer_date == false) ? (time()+(60*60*get_settings("gmt_offset"))) :newer_date;
since =newer_date – abs(strtotime(older_date));
//根据自己的需要调整时间段,下面的24则表示小时,根据需要调整吧
if(since < 60 * 60 * 24){
for (i = 0,j = count(chunks);i < j;i++)
{
seconds =chunks[i][0];name = chunks[i][1];
if ((count = floor(since / seconds)) != 0)
{
break;
}
}out = (count == 1) ? ‘1 ‘.name : "count {name}";
return out." 前";
}else{
the_time(get_option(‘date_format’));
}
}[/code]
2. 在需要调用时间的地方使用下面方式调用。
例如:显示为“发表于 XX小时前”则在对应的index.php, single.php之类的文件中修改为以下代码即可:
[code lang=”php”]发表于 <?php echo time_since(post->post_date);?>[/code]
完毕,大家拿去捣鼓吧~
本文链接:http://www.yunweipai.com/1475.html
刚好想实现这样的效果呢,收走了。
[…] http://blog.tektea.com/archives/1475.html 分类: 网络技术 日期: 2012年10月18日 by 石头. […]
这些小技巧我是不在乎的。哈哈。
我所有文章都是定时发布
时间的可读性更友好了