Display “Time Ago” for Posts and Comments in WordPress

Written by pelister. Posted in Wordpress

Most of the templates for WordPress displays author info, tags, date and sometimes time also in every posts. If you had ever thought of displaying time in facebook style, here is a solution for you.

Open functions.php, from the themes folder, fot the theme you wish to use this feature and add these lines.

function timeago( $type = 'post' ) {
	$d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
	return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');
}

then save your functions.php. This function will return the time ago the post was published. Add the following line wherever you need to display time ago.

<?php echo timeago(); ?>

You can add this to your single.php – usually for single posts or comments.php – for comments. Usage depends on your template design and the area of need.

Popular Posts

Techlister© - 2012