ArafatKn.Com

Go Ahead with the Power of Technology

Add “xx Times ago” Function without plugin on Your WordPress Theme

Today i am going to publish a php function for wp theme to show “xx hours ago”, “xx seconds ago”, “xx minitues ago”, “xx days ago” etc on wordpress post, comment etc.
First copy this code of php function to yours THEME functions.php file. Basically /wp-content/themes/YOUR-THEME/functions.php

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

Now your function is ready for call. This function has one parameter. Default parameter value is “post”. Now call this function under wp loop.

<?php echo time_ago("post"); ?>

For comment type

<?php echo time_ago("comment"); ?>

For post type you can use this,

<?php echo time_ago(); ?>

Thats all. You have done.
If you have problem doing this, comment here.

Leave a Reply

Your email address will not be published. Required fields are marked *

Personal Blog of ARAFAT ISLAM © 2016