I'm still trying to wrap my head around PHP (no pun intended) and am trying to get something to work using the Advanced Custom Fields plugin in WordPress.

So, I've got a repeater field that's made up of two Number Fields and a True/False field. I'm basically trying to write a statement like this:

If the True/False field is true then echo both sub_field_1 and sub_field_2, else echo just sub_field_1.

So far, I've got this:

while(has_sub_field('parent_repeater')) {

    if( the_sub_field('true') ) { 
      echo '<p>' . get_sub_field('sub_field_1') . get_sub_field('sub_field_2') . '</p>';

    else  {
      echo '<p>' . get_sub_field('sub_field_1') . '</p>';

What am I doing wrong? I'm currently going through the ACF documentation, if anyone could point me in the right direction, I'd really appreciate it.

Thanks in advance!


<ul class="list">

<?php while ( have_rows('blend') ) : the_row(); ?>

        $field = get_sub_field_object('blend');
        $value = the_sub_field('blend');
        $label = $field['choices'][ $value ];

        <span>Pct: <?php the_sub_field('percentage'); ?> Mix: <?php the_sub_field('mix'); ?></span>

<?php endwhile; ?>


<?php else :

echo '<p>No Rows found</p>';

endif; ?>

2 Answers 2

if( get_field('parent_repeater') ) : while( has_sub_field('parent_repeater') ) :

    $some_bool = get_sub_field('true');
    $sf1 = get_sub_field('sub_field_1');
    $sf2 = get_sub_field('sub_field_2');

    if( $some_bool == true ){
        echo $sf1 . $sf2;
    } else {
        echo $sf1;

endwhile; endif;

You must use the non-echoing version of the function. Use the get version. Also, I advise you to use safer name for fields. These seem a little dangerous. Maybe just me.


the_sub_field echos the value, so can't be used in a conditional. Use get_sub_field instead, which returns the value.

This is also the pattern for most of WordPress core, there are often get_ and the_ versions of functions which will return and echo values respectively.

  • I've edited my original question above, and tried to use get_sub_fieldinstead. Still though, I can't seem to get the value's label to appear. Any idea what I'm doing wrong?
    – realph
    Jan 3, 2014 at 16:38
  • same problem: $value = the_sub_field('blend'); you can't do variable assignment with the_, use get_
    – Milo
    Jan 3, 2014 at 16:42

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.