Jump to content

Archived

This topic is now archived and is closed to further replies.

bfrye26

how to add the time_ago to post loop

Recommended Posts

bfrye26

Hello,

 

I am looking to add in the time_ago into my post loop.

 

I have added the

 

function time_ago( $type = 'post' ) {

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

 

into my functions.php but I can not find out where to add the function into the post loop

 

any help would be amazing!

Share this post


Link to post
Share on other sites
Rob

I'm not sure what your goal is, ultimately.  I use this:  http://wordpress.org/plugins/dynamic-dates/, which though outdated, still works.  Its so basic there's little to go wrong.


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
bfrye26

Rob What I am trying to do is have it so posts will say (posted 30 minutes ago) etc

 

Seen on many news and game sites.

 

let me know your thoughts

Share this post


Link to post
Share on other sites
Danny

Hi,

Probably the simplest way would be to use the human_time_diff( $from, $to ) fuction and create your own callback function, then add it as a shortcode.


Please search our forums, before posting!

Share this post


Link to post
Share on other sites
bfrye26

Danny problem is I want it in the post loop, could this be done with shortcodes?

Share this post


Link to post
Share on other sites
Rob

You could add the shortcode to the post's meta info. That way it's right there, next to the date.  If that accuracy isn't available directly in DMS, enable v2 Compatibility then add it, save and disable v2 compatibility.  I doubt you'll have to go through such hoops. Should work in DMS.


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites
Danny

Yeah, all the Post meta data are shortcodes, so you can simply add your own via shortcode.


Please search our forums, before posting!

Share this post


Link to post
Share on other sites
anthalis

Hi there,

 

Try adding this function to your functions.php (I am assuming you used a child theme).

add_shortcode('time_ago', 'post_modified_shortcode');
function post_modified_shortcode($atts) {
	return human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago';
}

Then in any PostLoop section you can add this shortcode to the metabar [time_ago].

 

The shortcode also works inside the post content.

 

With the help of a filter or action, you should be able to use it in other places (before/after post content, before/after excerpts, etc) by using either 

echo do_shortcode('[time_ago]');

or

post_modified_shortcode();

in another function. 

 

Let us know if this is what you needed and if it works.

 

Regards,

Anca

Share this post


Link to post
Share on other sites
Rob

:D


Former PageLines Moderator, Food Expert and Raconteur

Share this post


Link to post
Share on other sites

×