Lê Nghĩa
Lê Nghĩa

Reputation: 255

Get post modified time in WordPress: X Minutes/Days/Years ago

This is my code:

add_filter('the_modified_time', 'dy');
 function dy() {
    $time = get_the_modified_time();
    $mytimestamp = sprintf(__('%s ago'), human_time_diff($time));
 return $mytimestamp;
}

It does not work as I would like. What I am doing wrong?

Any help is appreciated.

Upvotes: 0

Views: 569

Answers (1)

hostingutilities.com
hostingutilities.com

Reputation: 9549

Your filter only affects the the_modified_time() function. It could be that your theme is displaying post times with a different time function. Try using the following.

add_filter('get_the_modified_time', 'dy');
add_filter('get_the_modified_date', 'dy');
add_filter('get_the_date', 'dy');
add_filter('the_date', 'dy');
add_filter('get_the_time', 'dy');
add_filter('the_time', 'dy');
function dy() {
    $time = strtotime(get_post()->post_date);
    $time_ago = human_time_diff($time, current_time('timestamp'));
    return sprintf(__('%s ago'), $time_ago);
}

Upvotes: 2

Related Questions