Using PHP variable within ACF repeater field

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?

1 answer

  • answered 2019-08-15 06:20 naththedeveloper

    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')