Skip Navigation

[Résolu] Help with conditional targeting only part of a date field.

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have two custom date fields, one for the start date and one for the end date. If both dates are populated, and they both exist in the same months, then I want to display the date range like 1 - 4 June 2018. If the start and end dates fall in different months, then I want to show them like 28 June - 7 July 2018. So I need to be able to target the month name in a conditional.

Solution:
You can use the Types field shortcode to format the output of the date custom field so that it only includes the month name:

[types field='event-starts-date' format='F'][/types]

You can test those formatted custom field values in a conditional:

[wpv-conditional if="( '[types field='event-starts-date' format='F'][/types]' eq '[types field='event-end-date' format='F'][/types]' )" evaluate="false"]
Start and end date months do not match, so output the first month name here.
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/#date
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created Il y a 6 années et 5 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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 2 réponses, a 2 voix.

Dernière mise à jour par diyanK Il y a 6 années et 5 mois.

Assisté par: Christian Cox.

Auteur
Publications
#914210

Hi,

Here is my situation:

I have Events.
Some events are single day events, others span across more than one day.

I use two date fields, second one is conditionally triggered by a checkbox which asks me if the event is longer than a day.

If it is a single day event front-end only shows the first field (which is called "event-starts-date").
When I define the date of the event I use the checkbox(called "duration") which conditionally triggers the addition of the second date field(called "event-end-date") that will mark the event's end date (if the event is longer than 1 day).

Here is how my code looks:

[wpv-conditional if="( $(wpcf-duration) eq '1' )"]
     [types field='event-starts-date' style='text' format='j F Y'][/types]
[/wpv-conditional]
[wpv-conditional if="( $(wpcf-duration) eq '2' )"]
     [types field='event-starts-date' style='text' format=' j F'][/types] - [types field='event-end-date' style='text' format='j F Y'][/types]
[/wpv-conditional]

All works good and as expected.

In cases when the event spans across multiple days in a single month I get this result:
15 June - 16 June 2018
In cases when the event spans across two months I get this result:
28 June - 3 July 2018

Here is my problem:
I would like to keep the month titles when it spans across multiple months, but would like to remove only the first month title when the event is contained in a single month.
So it should work like this:
15 - 16 June 2018
28 June - 3 July 2018

Is there a way to use a conditional that will target only part of the date custom field, so that I can remove the first month title if it is equal to the month title within the second date custom field?

Thanks for looking into this!

#914457

You can use the Types field shortcode to format the output of the date custom field so that it only includes the month name:

[types field='event-starts-date' format='F'][/types]

You can test those formatted custom field values in a conditional:

[wpv-conditional if="( '[types field='event-starts-date' format='F'][/types]' eq '[types field='event-end-date' format='F'][/types]' )" evaluate="false"]
Start and end date months do not match, so output the first month name here.
[/wpv-conditional]
#914604
Screen Shot 2018-06-18 at 11.32.28.png

Hi Christian,

This is good!

I actually used the evaluate "true" to get it going.

Then I put this new code inside my other conditional that evaluates against being a multiple day event and it worked beautifully!

Here is my result: