简单的增强WordPress时间可读性的代码
把WordPress的默认的时间格式变为“xx之前”很简单,用到了human_time_diff( $from, $to='')函数,当然你可以写一个模版标签然去替换模版里默认的时间函数,但还有更简单的方法。
将下面的代码拷贝到主题的functions.php中,所有用the_date()或get_the_date()函数显示的时间都将变为“xx ago”的格式。
注意:使用改代码需要在设置»常规中选择一个英文文本的时间格式。
add_filter( 'the_date', 'human_readable_date');
add_filter( 'get_the_date', 'human_readable_date');
add_filter( 'the_modified_date', 'human_readable_date' );
add_filter( 'get_the_modified_date', 'human_readable_date' );
function human_readable_date( $the_date ){
return human_time_diff( strtotime($the_date) ) . __(' ago');
}
如果你在模版里写了自定义的时间,且想利用上面的代码变为可读性更好的时间格式,这样写即可,假设$date表示你要显示的时间
$date = '2013-08-07';
echo apply_filters('get_the_date', $date, '');
来自http://www.solagirl.net/wordpress-human-readable-date.html
