{% if acf.post_field({ "field": "event_date" })|date() %} / display condition in string, not element

I’m trying to only show the event end date when there is an end date. most events are 1 day and the end date is not set.

my other hurdle is that I cannot use the elements condition tool, because this is all entered into a text field.

is there a contextual variable that can be used for an empty string condition key? something like fallback="" possibly? right now, when the end date doesn’t exist, it uses today’s day/date in place.

this is what I’m working on:
{{dc:acf:post_field field="event_end_date" type='date' format="j"}}

but this is the entire field:
<span id='month'>{{dc:acf:post_field field="event_date" type='date' format="M"}}.</span>&nbsp;<span id='day'>{{dc:acf:post_field field="event_date" type='date' format="j"}}</span><span id='endday'>{{dc:acf:post_field field="event_end_date" type='date' format="j" fallback=""}}</span>&nbsp;<span id='years'>{{dc:acf:post_field field="event_date" type='date' format="Y"}}</span>

It sounds like you want to use Twig. Specifically the Flow Control section. When Twig is enabled it will output different types of Dynamic Content. So try a simple example and you can try expanding it to your other fields. Let us know if this helps.

{% if acf.post_field({ "field": "event_date" }) %}
Your output
{% endif %}

@Charlie, that does appear to be the path I want! (I guess I’m finally taking the dive :smiley:)

It took me a quick minute to figure this out. I finally figured it out by combining your content with the Twig content on date. & date filter is null display.
https://twig.symfony.com/doc/3.x/filters/date.html

The only part I can’t figure out is how to only display the DAY number, rather than the entire date.

this statement will only display content if the value is not null:
{{ acf.post_field({ "field": "event_end_date" }) is empty ? "" : acf.post_field({ "field": "event_end_date" }) }}

if I understand correctly, adding this |date() piece should provide the result I’m looking for, but it instead breaks the code
{{ acf.post_field({ "field": "event_end_date" }) is empty ? "" : acf.post_field({ "field": "event_end_date" })|date("j") }}

any idea what I might be doing wrong?

BTW… this is SUPER COOL! thank you for all of your hard work!!!

1 Like

Hello @splaquet,

You are most welcome.

Just want to comment on the Twig statement. The default one can be:
{{ acf.post_field({ "field": "event_end_date" })|default("")|date("j") }}

If you want to store the value first, you may have something like:

{% set end_date = acf.post_field({ "field": "event_end_date" }) %}
{{ end_date is empty ? "" : end_date|date("j") }}

And when using if statement as pointed out by @Charlie, you may have something like:

{% set end_date = acf.post_field({ "field": "event_end_date" }) %}
{% if end_date is not empty %}
    {{ end_date|date("j") }}
{% endif %}

Hope this helps.

I’ve tried every combination of the provided examples. every time I try and customize the date display, that Twig rendering error pops up. any suggestions?

Is there any way your ACF field can return a different time format? It looks like Twig is getting held up by the fact you are starting with the day of the month.

I was coming back to report that any | date() value creates that twig template error., including | date() without any value. would you assume that’s also part of the date format?

I’m going to look and see if I can modify the date format in ACF.

holy shit, that was it. I switched it from the default to m/d/Y and it immediate started to work.

I feel bad for wasting your time, but truly appreciate your help!