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

Need help with ACF repeater field. WordPress

  • SOLVED

I have an ACF repeater field with five possible inputs. They are:

service (text field)
service_page_link (ACF page link field)
competitor_one (checkbox with one option of "Yes")
competitor_two (checkbox with one option of "Yes")
competitor_three (checkbox with one option of "Yes")

I'm trying to show the first service text field wrapped in an a href using the service_page_link field.

Then I'm trying to show a value for the next three fields based on whether the "Yes" checkbox is checked or not.

Here's the code that's partially working -- it is printing out the competitor one through three part, but not the service a href part.




<?php

$lists = get_field( 'service_comparison' );

if( $lists ){

foreach( $lists as $list ){

echo '<div style="float:left;width:25%;"><a href="';
service_page_link;
echo '">';
service;
echo '</a></div>';

if( $list['competitor_one'] ){

echo '<div style="float:left;width:25%;">Yeah baby</div>';

}

else {

echo '<div style="float:left;width:25%;">Not Checked New</div>';

}

if( $list['competitor_two'] ){

echo '<div style="float:left;width:25%;">Yeah baby</div>';

}

else {

echo '<div style="float:left;width:25%;">Not Checked New</div>';

}

if( $list['competitor_three'] ){

echo '<div style="float:left;width:25%;">Yeah baby</div>';

}

else {

echo '<div style="float:left;width:25%;">Not Checked New</div>';
echo '<div class="clear"></div>';

}

}

}



?>

Answers (4)

2015-05-20

Andrea P answers:

Hello Kyler,

in the a href code you have not called the variables but just plain text. you have to use $ otherwise php won't consider the text as a variable and just print the text. in any case, when you loop the repeater in a foreach, you are getting repeater's rows. so you get a $list array which contains all the values of the row fields, placed in this way:

$row = array( 'FIELD_NAME' => 'FIELD_VALUE', 'FIELD_NAME' => 'FIELD_VALUE', 'FIELD_NAME' => 'FIELD_VALUE');

so you can access the field in this way:
$row['FIELD_NAME']

also, there was the classic mistake when using checkboxes. ACF stores checkboxes as arrays, one element for each checkbox option. so even if you have only one option, you have to treat the checkbox field value as an array of sub-values.


I think this code will work for you.
cheers!


<?php
$lists = get_field( 'service_comparison' );
if( $lists ){
foreach( $lists as $list ){
// when looping repeater rows in this way, the fields content is store as element of the row array. so the row array is $list, as stated above in the foreach declaration, and then you can access your fields with $list['FIELD_NAME']
echo '<div style="float:left;width:25%;"><a href="'. $list['service_page_link'] .'">'.$list['service'] .'</a></div>';

// checkboxes in ACF are stored as Arrays, one element for each option. so in this case, the checkbox value will be an element of a sub array of $list. as you have only one option for each checkbox (Yes), you can easily access it using the array index [0] (which is the first element of the array)
if( $list['competitor_one'][0] == "Yes" ){
echo '<div style="float:left;width:25%;">Yeah baby</div>';
}
else {
echo '<div style="float:left;width:25%;">Not Checked New</div>';
}
if( $list['competitor_two'][0] == "Yes" ){
echo '<div style="float:left;width:25%;">Yeah baby</div>';
}
else {
echo '<div style="float:left;width:25%;">Not Checked New</div>';
}
if( $list['competitor_three'][0] == "Yes" ){
echo '<div style="float:left;width:25%;">Yeah baby</div>';
}
else {
echo '<div style="float:left;width:25%;">Not Checked New</div>';
echo '<div class="clear"></div>';
}
}
}

?>


Kyler Boudreau comments:

Andrea, it's not every day that someone's code works flawlessly, but your's did. Thanks for the explanation...it's why I tried your answer first, and then didn't need to try another. Voting for you.

2015-05-19

tom-hkwebs answers:

Hello Kyler,

have you tried to echo the 'service' and 'service_link_url' value with :


echo $list['service_page_link']; // echoes 'service_page_link' input value
echo $list['service']; // echoes 'service' input value


using these codelines in your script above like this, for example :


<?php
$lists = get_field( 'service_comparison' );
if( $lists ){
foreach( $lists as $list ){
echo '<div style="float:left;width:25%;"><a href="';
echo $list['service_page_link'];
echo '">';
echo $list['service'];
echo '</a></div>';
if( $list['competitor_one'] ){
echo '<div style="float:left;width:25%;">Yeah baby</div>';
}
else {
echo '<div style="float:left;width:25%;">Not Checked New</div>';
}
if( $list['competitor_two'] ){
echo '<div style="float:left;width:25%;">Yeah baby</div>';
}
else {
echo '<div style="float:left;width:25%;">Not Checked New</div>';
}
if( $list['competitor_three'] ){
echo '<div style="float:left;width:25%;">Yeah baby</div>';
}
else {
echo '<div style="float:left;width:25%;">Not Checked New</div>';
echo '<div class="clear"></div>';
}
}
}
?>

2015-05-20

Ellah Alzona answers:

Do you mean everything starting from the "service" field is a repeater? or only the 3 competitors are the repeater?

2015-05-20

Arnav Joy answers:

Hello Kyler ,

is this problem solved?