Ask your WordPress questions! Pay money and get answers fast! (more info)

Need if then statement in ACF repeater field WordPress

  • SOLVED

I've got a repeater field that has three checkboxes. I want to show text based depending on whether the checkboxes are checked or not.

This code works to do something is a check box is checked:



<?php

$lists = get_field( 'service_comparison' );
if( $lists ){
foreach( $lists as $list ){
if( $list['competitor_one'] ){
echo '<p>Yeah baby</p>';
}
}
}

?>



With the above code, if my first checkbox called "Competitor One" is checked, then it does display "Yeah Baby." That's working.

But I need an else statement to show different text if the field is not checked.

ALSO, this is just one checkbox. I have two others in the repeater field: competitor_two and competitor_three.

Thanks!

Answers (2)

2015-05-19

tom-hkwebs answers:

Hello,

maybe you can slightly modify the code provided by Jayaram Y to the following :


<?php

$lists = get_field( 'service_comparison' );
if( $lists ){
foreach( $lists as $list ){
if( $list['competitor_one'] ){
echo '<p>Yeah baby</p>';
}
else {
echo '<p>Competitor one not checked !</p>';
}
if( $list['competitor_two'] ){
echo '<p>Yeah baby two</p>';
}
else {
echo '<p>Competitor two not checked !</p>';
}
if( $list['competitor_three'] ){
echo '<p>Yeah baby three</p>';
}
else {
echo '<p>Competitor three not checked !</p>';
}
}
}

?>


Another workaround if the code above is not working : you seem to be using the ACF plug-in, which offers built-in functions to handle repeater field... Maybe something like following :


<?php if( have_rows('service_comparison') ): ?>

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

if( get_sub_field('competitor_one') {
echo '<p>Yeah baby</p>';
}
else {
echo '<p>Competitor one not checked !</p>';
}
if( get_sub_field('competitor_two') ){
echo '<p>Yeah baby two</p>';
}
else {
echo '<p>Competitor two not checked !</p>';
}
if( get_sub_field('competitor_three') ){
echo '<p>Yeah baby three</p>';
}
else {
echo '<p>Competitor three not checked !</p>';
}

?>

<?php endwhile; ?>

<?php endif; ?>


...assuming the get_sub_field() function does return boolean true if the checkbox is checked (and not for example the text value of the checkbox which would also get evaluated to true, resulting <p>Yeah baby</p> to be echoed even if the checkbox is NOT checked...)


Kyler Boudreau comments:

Tom, your code did the trick. Thanks man. BTW... the second set of ACF specific code didn't work (I am using ACF) but the first section of code you provided worked great. Thank you!

2015-05-19

Jayaram Y answers:

try this


<?php
$lists = get_field( 'service_comparison' );

if( $lists ){

foreach( $lists as $list ){

if( $list['competitor_one'] ){

echo '<p>Yeah baby</p>';

}
else if( $list['competitor_two'] ){

echo '<p>Yeah baby two</p>';

}
else echo '<p>Yeah baby three</p>';
}

}

?>


Kyler Boudreau comments:

Jayaram,

Thanks. That code is close, but let me explain better:

The repeater field is called 'service_comparison.'

Inside it is a description text field called

service

and then three checkboxes:

competitor_one
competitor_two
competitor_three

each checkbox needs its own if and else statement to show text if the box is checked or text if the box isn't checked.