Skip Navigation

[Résolu] Conditional output related to date fields

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

Problem:

I have a CPT that include 2 dates, one start date, one end-date.
Displaying the output is pretty simple and easy, but I would love to do this :

IF month from start_date is different month from end_date, do something
IF month from start_date is equal month from end_date, do something else
So i think I first need to get the data out of the date fields to be able to compare the month and that is where I am stuck

Solution:

You can use Types shortcode to output the month value of custom date field by setting the "format" attribute, for example:
[types field="start_date" format="m"][/types]
[types field="end_date" format="m"][/types]
https://toolset.com/documentation/customizing-sites-using-php/functions/#date
format:
any valid WordPress date format

Then you can use them in the Views shortcode [wpv-conditional] to check if the month values are equal, for example:

[wpv-conditional if="( '[types field="start_date" format="m"][/types]' eq '[types field="end_date" format="m"][/types]' )"] 
do something
[/wpv-conditional]
[wpv-conditional if="( '[types field="start_date" format="m"][/types]' eq '[types field="end_date" format="m"][/types]' )" evaluate="false"] 
do something else
[/wpv-conditional]

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

50% of people find this useful.

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

Marqué : ,

This topic contains 3 réponses, has 2 voix.

Last updated by Luo Yang Il y a 6 années et 3 mois.

Assisted by: Luo Yang.

Auteur
Publications
#606580

***Tell us what you are trying to do?***
Hi, I have a CPT that include 2 dates, one start date, one end-date.
Displaying the output is pretty simple and easy, but I would love to do this :

  • IF month from start_date is different month from end_date, do something
  • IF month from start_date is equal month from end_date, do something else

So i think I first need to get the data out of the date fields to be able to compare the month and that is where I am stuck

*** Is there any documentation that you are following? ***
None that i can found

*** What is the link to your site? ***
Sample in the left column
hidden link

*** THANK YOU FOR YOUR HELP ***

#606647

Dear Louis,

You can use Types shortcode to output the month value of custom date field by setting the "format" attribute, for example:
[types field="start_date" format="m"][/types]
[types field="end_date" format="m"][/types]
https://toolset.com/documentation/customizing-sites-using-php/functions/#date
format:
any valid WordPress date format

Then you can use them in the Views shortcode [wpv-conditional] to check if the month values are equal, for example:

[wpv-conditional if="( '[types field="start_date" format="m"][/types]' eq '[types field="end_date" format="m"][/types]' )"] 
do something
[/wpv-conditional]
[wpv-conditional if="( '[types field="start_date" format="m"][/types]' eq '[types field="end_date" format="m"][/types]' )" evaluate="false"] 
do something else
[/wpv-conditional]

https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-conditional

#606811

I really thank you, I haven't seen the output of the date and it's pretty amazing what conditional can do when well implement.
For future work I will now remember that I can use shortcode in conditional output!

#606949

You are welcome

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.