How can i put "posted x minutes ago on my posts or custom post type?

How can i put "posted x minutes ago on my posts or custom post type?


Wordpress has a built-in funtion called human_time_diff() that allows you to do this.

Just place this code in your 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');

}

To use it anywhere in your theme (for example, on single.php), place this code where you want the "time ago" feature displayed:

<?php echo time_ago(); ?>

Let me know if it worked for you.

How can i put "posted x minutes ago on my posts or custom post type? How can i put "posted x minutes ago on my posts or custom post type? Reviewed by TechTubeHQ on December 19, 2022 Rating: 5

No comments:

Powered by Blogger.