0

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:

<?php       
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!

EDIT:

<ul class="list">

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

    <?php
        $field = get_sub_field_object('blend');
        $value = the_sub_field('blend');
        $label = $field['choices'][ $value ];
    ?>

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

<?php endwhile; ?>

</ul>

<?php else :

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

endif; ?>
2
<?php
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.

0

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 '14 at 16:38
  • same problem: $value = the_sub_field('blend'); you can't do variable assignment with the_, use get_ – Milo Jan 3 '14 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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