L&L Set/Get for ACF Relationship Loop

Hello,

This is a weird one, the ACF Relationship loop returns nothing when the ACF field name comes from L&L Set/Get.

Here’s my code:

<Set name="field_name">
  <If singular type="type_1">
   field_1
  <Else if singular type="type_2" />
   field_2
  </If>
</Set>

  <Loop acf_relationship="{Get field_name}">
    <div><Field title /></div>
  </Loop>

<Get name="field_name" />

{Get field_name} in the ACF Relationship loop is uneffective.

While <Get name="field_name" /> at the end of the template returns field_name's value alright.

I tried different syntaxes, all fails:

<If singular type="type_1">field_1<Else if singular type="type_2" />field_2</If> (single line)

<Loop acf_relationship={Get field_name}>

<Loop acf_relationship={Get name='field_name'}>

Try putting everything from the opening to the closing set tag all on one line, like this

<Set name="field_name"><If singular type="type_1"> field_1<Else if singular type="type_2" />field_2</If></Set>

Line breaks in HTML are interpreted as spaces, which means that as soon as there’s a line break in the code editor within your Set tag, it’ll save a space into the variable, which means that your loop ends up looking something like this:

  <Loop acf_relationship=" field_1 ">

Which I assume wouldn’t work right.

This is just how HTML works so I’m not sure if there’s a way to address this in L&L, but I made a feature request a while back to at least prompt people when there’s a risk that a space character will unintentionally make its way into a variable.

Let me know if that doesn’t solve your issue and I can look a little deeper into this.

2 Likes

Hi @benjamin, thank you.

Nice catch, you’re right, it works without line breaks.
I tried that already (see above) but didn’t include the <Set> paired tags in the same single line as the conditional.

2 Likes