1

When using ACF (Advanced Custom Fields) repeater fields, the markup should look like this to initiate the loop.

<? if( have_rows('my-repeating-field) ): ?>
<? endif ?>

However, I would like to make this dynamic, meaning I use another PHP variable as part of the if() code, something like as follows:

<? $variable = get_field('my-variable-field'); ?>

<? if( have_rows("'" . $variable . "-repeating-field" . "'") ): ?>
<? endif ?>

However, this is not working. Is there a way I can do this with ACF/PHP? Seems pretty simple to me?

6
  • did you checked $variable has any value or not? you can try $row_field = $variable . '-repeating-field'; and then use var_dump to check if it has corrected value or not. then if it is correct try if(have_rows($row_field)... Commented Aug 14, 2019 at 13:28
  • Yes it outputs as expected, but still doesn't work. I.e <? $variable . '-repeating-field' = variablevalue-repeating-field ?> Commented Aug 14, 2019 at 13:31
  • Is "variablevalue-repeating-field" an ACF repeater field name? Commented Aug 14, 2019 at 13:58
  • You're quoting the string in a weird way, so the value will have single quotes in it... have_rows($variable . '-repeating-field') should be enough Commented Aug 14, 2019 at 14:14
  • Cheers @naththedeveloper - this works! If you want to put it into an answer I'll mark as accepted. Commented Aug 14, 2019 at 14:46

1 Answer 1

2

The problem is that you're concatenating the string in a way which would cause the end value being passed to have_rows to have quotes inside of it. You only need to concatenate the value and the end of the string, and that should be enough.

have_rows($variable . '-repeating-field')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.