Display the field of a loop only it differs from the previous field

Hi there (it’s me again :sweat_smile:),

I’m trying to accomplish something, and despite going through the documentation and trying various approachs, I can’t seem to get a hang of it.

I have a table in which I’m looping through an ACF repeater including dates. This table displays dates, but I’d like to have a raw indicating the name of the month, but only once when the month switches. For this, I’d need to somehow compare the month value of the ACF field and have them displayed only once until there is a change.

So far, I only manage to display them all of the time.


I tried to create an condition, and felt like I had to create a list to be able to compare items between each other. Although, I can’t find how to compare an item to the next.
I guess what I’d like to do is something like (improper markup incoming):

 <If ?item_value? is_not ?previous_item_value?> 
<Field acf_date_time=field_name format="F" />
</If>

Is there anyway to achieve that? Maybe using the Math tag?

The first thing that comes to mind would be to use Set and Get. This might not be the most performant approach, but I think it would be quite simple. The contents of the loop repeat once for each item in the loop, right? So at the end of the loop you could have <Set month_variable><Field your_month_field /></Set> and then the next time the loop runs, you could have <If variable=month_variable is_not value="{Field your_month_field}"> which would check if the variable (which was set the last time the loop ran) matches the current loop item’s your_month_field field. If that conditional logic is true (the values don’t match) that’d mean your in a new month so you’ll want to display a month row. Else, display your other row content.

There might be a slightly more efficient way to achieve this if you can somehow create a loop within a loop where the outside one loops through all the month values and the inner one loops through the items that have the current month in their field value. This kind of approach would be doable with a custom post type, but I’m not sure if you can set query parameters that give you that kind of control on an ACF repeater loop. Might not be worth the hassle anyway, just thought I’d express some of the ways you could think about approaching this.

Hi Benjamin,

Thanks a lot for your insight, you were, as always, right!
Your first approach is indeed sufficient, so I used Set and Get and it works properly. I had to adjust a bit though because my date field is not a month field, so I filtered the month bot in the Set and in the Get part just so:

<Set month_variable>
<Date format="F"><Field crs_date_time /></Date>
</Set>

<If variable=month_variable is_not value="{Date format=F}{Field crs_date_time}{/Date}">

This last one feels a bit weird so I wonder if this is not again some custom markup I improvised, but it is working!

Thanks a lot for taking the time, it helps me a lot

1 Like

Nah, that looks perfect! I know what you mean though, it feels a little odd to use a whole bunch of tags in the value of an attribute like you’ve done with value="{Date format=F}{Field crs_date_time}{/Date}" but that is the correct syntax. You’re just swapping <> for {} and L&L figures out the rest. Glad you were able to get this working!

1 Like