Navigation überspringen

[Gelöst] Calculating post(s) age

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

Problem: I would like to calculate the age of a post, in number of days, based on its publish date, and display that number for each post in a View of posts.

Solution: It would require a custom shortcode to calculate the difference between today's date and the post's publish date and display that in a View, something like this:

add_shortcode( 'num-days', 'wpv_num_days_shortcode');
function wpv_num_days_shortcode( $atts ) {
  $atts = shortcode_atts( array(
      'postid' => ''
  ), $atts );
  $timestamp = get_the_date( 'U', $atts['postid'] );
  if( $timestamp != '') {
    $age = floor((time() - $timestamp) / ( 60*60*24 )) ;
    return $age;
  }else {
    return 0;
  }
}

Then in the front-end the shortcode is like this:

[num-days postid="[wpv-post-id]"]
This support ticket is created vor 5 Jahren. 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Dieses Thema enthält 22 Antworten, hat 3 Stimmen.

Zuletzt aktualisiert von Christian Cox vor 4 Jahren, 12 Monaten.

Assistiert von: Christian Cox.

Author
Artikel
#1416693

My issue is resolved now. Thank you!

#1416791

Christian, Sorry, one more question.

If I wanted to apply this to calculate the oldest published defect for one view how would I do that?

Thanks!

#1417077
Screen Shot 2019-12-19 at 3.54.05 PM.png
Screen Shot 2019-12-19 at 3.55.29 PM.png

I think the most straightforward way is to reuse the View you want to calculate, setting a limit of 1 result and setting the sort order to post date ascending. You can use shortcode attributes or View block attributes to modify the limit and sort order if you don't want to duplicate the original View or create a whole new View just for the oldest age. See the screenshots here, you can see how limit and offset can be implemented in both the blocks builder and the Fields and Views popup in a Classic editor.

Then when you check the View on the front-end, only the 1 oldest result should appear. With a bit of CSS you could hide all the other information and simply show the age alone. Otherwise, you would have to create an entirely separate View just for showing the oldest age from one View.

#1418561

Actualy what I had in mind was not showing the result but replacing them with an image that would link back to the oldest result.

#1420737

I'm sorry I don't understand. Replacing what with what image? Can you show me more details about this please?

#1424885

Similar to what I've done on the homepage with the 'alert' image on top. So if there are items that exists for a very long time it would display an alert and you could click on the image and view that item.

#1426363

Minesh
Supporter

Sprachen: Englisch (English )

Zeitzone: Asia/Kolkata (GMT+05:30)

Christian is on vacation. He will get back to work on the upcoming Sunday.

I see there ticket is re-opened with the new question and I would like to clarify here where exactly with what section you need further assistance.

If you require urgent help, Can you please share problem URL and clarify what exactly the solution you need and I will work on it, otherwise, Christian will get back to you on this Sunday.

#1430221

So you would create a new View that shows the oldest item (sorted by publish date, ascending, limit 1). In the Loop Editor panel, you can click "Skip the wizard" and manually construct the image link inside the wpv-loop tags. You can use the wpv-post-url shortcode to set the link's href attribute:

<a href="[wpv-post-url]">
  <img src="<em><u>versteckter Link</u></em>" />
</a>

Modify the image src, and this will show an image as a link to the oldest result.