I have a site that is displaying ferry schedules.
The repeating field group contains the following elements:
• Departure Time
• Arrival Time
• Display Time (the time of day that this group should start being highlighted)
What I would like to do is add the class "next-trip" to the field group that is between the "Display time" and the Departure time, so I can animate it with CSS.
I am including some screen shots to help.
My site is currently under local development.
Hi, there's nothing exactly like this built-in to Types, so you'll need some custom code. I suspect the best way to do this is to create a custom shortcode that returns the "next-trip" class name based on some conditionals that analyze the values of the two custom fields, related to the current time. I can show you how to set up a custom shortcode and show you how to get the values of the custom fields within that shortcode, then you can set up whatever custom date/time manipulation you need to convert those times into a format that is comparable to the local server time. Something like this:
/* --------------------------------------------- */
// CUSTOM SHORTCODE TO RETURN NEXT-TRIP CLASS
add_shortcode( 'get-next-trip-class', 'get_next_trip_class_func');
function get_next_trip_class_func($atts)
{
$post_id = get_the_id();
$display_time = get_post_meta( $post_id, 'wpcf-displayfieldslug', true ); // something like 4:00
$departure_time = get_post_meta( $post_id, 'wpcf-departurefieldslug', true ); // something like 5:00
$current_timestamp = date('U'); // unix timestamp of the current date/time
// custom code here to convert display time and departure time to unix timestamps
if( ... custom code comparing display, current, and departure timestamps ... ) {
return 'next-trip';
}
return;
}
Not sure of your level of comfort with custom code, this is just a general outline of what I was envisioning.
This looks like a plan it makes sense to me.
I am a Toolset user because I am not that comfortable with PHP, enough knowledge to get into trouble. So I could definitely use some more assistance.
My custom fields are text fields (I can not wait till Toolset has a time field, would be so helpful)
I have changed the WordPress time settings to (H:i)
I added the custom fields to your code:
/* --------------------------------------------- */
// CUSTOM SHORTCODE TO RETURN NEXT-TRIP CLASS
add_shortcode( 'get-next-trip-class', 'get_next_trip_class_func');
function get_next_trip_class_func($atts)
{
$post_id = get_the_id();
$display_time = get_post_meta( $post_id, 'wpcf-display-time', true ); // something like 4:00
$departure_time = get_post_meta( $post_id, 'wpcf-departure', true ); // something like 5:00
$current_timestamp = date('U'); // unix timestamp of the current date/time
// custom code here to convert display time and departure time to unix timestamps
if( ... custom code comparing display, current, and departure timestamps ... ) {
return 'next-trip';
}
return;
}
I am a Toolset user because I am not that comfortable with PHP, enough knowledge to get into trouble. So I could definitely use some more assistance.
Understood, but unfortunately it's not a simple problem to solve and it's mostly unrelated to Toolset. I can show you how to use Toolset APIs to extract information from Toolset custom fields, explain the format of the data you'll retrieve, and point you towards similar examples. What you do with that Toolset data, after you retrieve it, is unrelated to Toolset and outside the scope of our support policy. Our support policy says we do not develop custom code solutions for you: https://toolset.com/toolset-support-policy/
If you want to explore PHP topics related to this:
https://www.php.net/manual/en/function.strtotime.php
https://stackoverflow.com/questions/113829/how-to-convert-date-to-timestamp-in-php
https://www.tutorialrepublic.com/faq/how-to-convert-a-date-to-timestamp-in-php.php
Questions about formatting dates based on strings like "6:00", translating them by timezone, and comparing them in PHP would be better posed in a PHP support forum, like StackOverflow, or something similar. The solution requires understanding PHP dates, times, timezones, formatting, and manipulation...I have no simple cut-and-paste solution available here. If you're not comfortable with PHP or the concepts involved here, I suggest you contact an independent contractor who knows PHP and understands date/time/timezone manipulation.
We have a couple of links available where you might be able to connect with an independent developer:
https://toolset.com/contractors
https://codeable.io/developers/toolset/
I will look into that.
The only reason it is related to Toolset is that there is no time field. I have seen many requests for one as it is something that is often needed to be dealt with.
I've added your vote to the internal ticket we have for adding a time-only custom field type. Feel free to reopen here or open a new ticket if you need more information about extracting information from a Types custom field using the PHP API.