Problem: I have a repeating "date" type custom field. I would like to sort the dates in chronological order, and I would like to apply a strike-through style to the dates that occurred in the past.
Solution:
Add this to functions.php:
add_shortcode( 'sort_and_format_post_date_fields', 'sort_and_format_post_date_fields_func'); function sort_and_format_post_date_fields_func($atts = []) { $atts = shortcode_atts([ 'postid' => '', 'field' => '', 'format' => 'l F jS Y', ], $atts); $values = get_post_meta($atts['postid'], 'wpcf-my-date'); usort($values, 'numbersort'); $string = ''; foreach($values as $value) { $date = date_i18n($atts['format'], $value); $prefix = $value < strtotime('0:00:00') ? '<span style="text-decoration:line-through;">' : ''; $suffix = $value < strtotime('0:00:00') ? '</span>' : ''; $string .= ($string == '' ? '' : ', ') . $prefix . $date . $suffix; } return $string; } function numbersort($a, $b) { $res = 0; if ( $a > $b) { $res = 1; } return $res; }
Then use it by placing this in the content of the post that includes the date field:
[sort_and_format_post_date_fields postid="[wpv-post-id]" field="my-date" format="l F jS Y"]
Relevant Documentation:
https://toolset.com/documentation/user-guides/repeating-fields/
https://codex.wordpress.org/Shortcode_API
http://php.net/manual/en/function.date.php
https://codex.wordpress.org/Function_Reference/date_i18n
Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.
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)
Ce sujet contient 8 réponses, a 2 voix.
Dernière mise à jour par Il y a 7 années.
Assisté par: Christian Cox.
Le forum ‘Types Community Support’ est fermé à de nouveaux sujets et réponses.