Skip Navigation

[Resolved] Shortcode for 'x amount of time ago' not working

This support ticket is created 4 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Nigel 4 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#1410449

In my view loop I want to display the time that has passed since adding this particular post (e.g. 5 minutes ago / 16 days ago / etc.). I added a shortcode that does the calculation:

add_shortcode('time-ago', 'time_ago_shortcode');
function time_ago_shortcode($atts, $content = null) {
return human_time_diff(wpv_do_shortcode($content)) . ' geleden';
}

In the view loop using the block editor I added this line of code:

[time-ago][wpv-post format="U"][/time-ago]

The result I am getting for each post however is 50 years ago. Any idea what I am doing wrong?

For reference I am following this thread:
https://toolset.com/forums/topic/shortcode-for-current-time-modified-date/

Thanks lots,
Rens

#1411237

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Is it as simple as you are using a "wpv-post" shortcode, where presumably you mean wpv-post-date?

[time-ago][wpv-post-date format="U"][/time-ago]

https://toolset.com/documentation/user-guides/views/views-shortcodes/#wpv-post-date

#1411567

Hi Nigel,

I'll have to time to check it tonight!

Cheers,
Rens

#1411653

Thanks Nigel, it works now. So simple... You're a legend!

#1411655

Actually, it worked fine in the wp dashboard, but actually viewing this on the frontend the whole site appears to be broken..?

So when I paste in your code it looks exactly like I wanted it to in the block editor. However trying to view any page on the site on multiple devices the whole site looks broken. I can fix it by removing the code. Any clue what could be causing this?

Rens

#1411803

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

When you say "looks broken" what do you mean?

You probably don't need to use the custom Views shortcode parser (wpv_do_shortcode), you could try replacing that with the native WordPress do_shortcode() function, e.g.

add_shortcode('time-ago', function ($atts = [], $content = null) {
    
    return human_time_diff( do_shortcode($content) );

});

I can't see anything in that which would create problems.