Tell us what you are trying to do?
We have set up a Custom Post Type with a repeatable section for courses. In these repeatable sections we have date fields for when the course is out of date. We would like a countdown for each course that will say 45 days to go or if it has passed to say Expired 12 days ago.
Is this something that is possible?
Thanks
Is there any documentation that you are following?
Is there a similar example that we can see?
What is the link to your site?
Hi Jack,
Thank you for waiting.
Based on what you've shared, you can update the code snippet from the other ticket, to include the number of days comparison, like this:
add_shortcode("show_day_difference", "show_day_difference_func");
function show_day_difference_func($atts) {
$a = shortcode_atts( array(
'date' => ''
), $atts );
// today's date
$current_date = date( 'U' );
// date to compare
$compare_date = $a['date'];
// difference in dates
$datediff = $compare_date - $current_date;
// difference in number of days
$datediffdays = round($datediff / (60 * 60 * 24));
// conditional display of difference based on number of days
if( ($datediffdays == 0) || ($datediffdays == 1) ) {
$output = '<div class="date-diff-orange">Expires in 1 day</div>';
}
elseif ($datediffdays == -1) {
$output = '<div class="date-diff-red">Expired 1 day ago</div>';
}
elseif ($datediffdays < -1) {
$output = '<div class="date-diff-red">Expired '.abs($datediffdays).' days ago</div>';
}
elseif ( ($datediffdays >= 2) && ($datediffdays <= 190) ) {
$output = '<div class="date-diff-orange">Expires in '.$datediffdays.' days</div>';
}
elseif ( $datediffdays > 190 ) {
$output = '<div class="date-diff-green">Expires in '.$datediffdays.' days</div>';
}
return $output;
}
The above code snippet can be added into the active theme's "functions.php" file and this is how you can pass on the date field's value in the shortcode:
[show_day_difference date="[types field='date-field-slug' output='raw'][/types]"]
Please replace "date-field-slug" with the actual slug of the date field used on your website and note that shortcode will output the result, inside special classes, based on the number of days e.g. "date-diff-orange", "date-diff-green" & "date-diff-red", which can be used to style them differently.
I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Hi there,
I have added the code into functions.php and the shortcode into the view.
But they all say Expired 18163 days ago (see attached)
Have I done something wrong?
Hi Jack,
Can you please share temporary admin login details and the link to a page where this view can be seen?
Note: Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.
regards,
Waqar
Hi Jack,
Thank you for sharing the login details.
I've noticed that you're using the new shortcode in the view "04 - Qualification List" and two items, need to be fixed:
1. In the view's "Content Selection" section, please select "Qualification Upload", instead of "Qualifications".
( screenshot: hidden link )
2. In the "Loop item in 04 - Qualification List" section, please update the correct slug of the date field "qualification-expiry-date".
( screenshot: hidden link )
regards,
Waqar
Hi there,
Thank you for sorting this... I was just wondering if there was a way that if the date is not set It will hide the HTML code?
Thanks,
Jack