Home › Forums › Add-ons › Repeater Field › FOR EACH Inside of an IF/ELSE using Select Field/Repeater
Maybe it’s a limitation of PHP knowledge, but is there a clean way to have a Repeater Field inside of an IF/ELSE.
Basically we have a testimonial panel that has a Select to display either a background image or background color. Then within each output we want to display a testimonial repeater field.
Code Below
<?php
if ($section['background_image_or_color'] == 'Image') {
?>
<section id="testimonial-slideshow" class="background-cover" style=" background-repeat: no-repeat; background-image: url('<?=$section['image']['sizes']['image-1280-460']?>'); data-stellar-background-ratio="0.75">
<h2 class="text-center"><?=$section['title']?></h2>
<?php
foreach ($section['testimonials'] as $item => $block) :
?>
<div class="testimonial-slideshow__slide background-cover">
<div class="testimonial-slideshow__slide-content">
<?=$block['testimonial_text']?>
<h5>- <?=$block['testimonial_attribution']?></h5>
<figure><?=$block['testimonial_image']?></figure>
<a class="button caps outline" href="<?=$section['button_link']?>"><?=$section['button_text']?></a>
</div>
</div>
<?php endforeach; ?>
<ul class="testimonial-slideshow--nav">
</ul>
</section>
<?php
if ($section['background_image_or_color'] == 'Color') {
?>
<section id="testimonial-slideshow" class="<?=$section['background_color']?>"">
<h2 class="text-center"><?=$section['title_text']?></h2>
<?php
foreach ($section['testimonials'] as $item => $block) :
?>
<div class="testimonial-slideshow__slide background-cover">
<div class="testimonial-slideshow__slide-content">
<?=$block['testimonial_text']?>
<h5>- <?=$block['testimonial_attribution']?></h5>
</div>
</div>
<?php endforeach; ?>
<ul class="testimonial-slideshow--nav">
</ul>
</section>
<section id="testimonial-slideshow"<?php
if ($section['background_image_or_color'] == 'Image') {
?> class="background-cover" style="background-repeat: no-repeat; background-image: url('<?php
echo $section['image']['sizes']['image-1280-460']; ?>');" data-stellar-background-ratio="0.75"<?php
} else {
?> class="<?php echo $section['background_color']; ?>"<?php
}
?>>
<h2 class="text-center"><?php echo $section['title']; ?></h2>
<?php
foreach ($section['testimonials'] as $item => $block) {
?>
<div .... the rest of your code in here
<?php
} // end foreach testimonial
?>
</section>
Thank you so much! That worked!!! I really appreciate it!
This code has brought up one other question – is it possible to add a conditional within the repeater. Is there a specific syntax to be used?
I have tried multiple ways of writing it with no luck.
<?php
foreach ($section['testimonials'] as $item => $block) :
?>
<div class="testimonial-slideshow__slide background-cover">
<div class="testimonial-slideshow__slide-content">
<?=$block['testimonial_text']?>
<h5>- <?=$block['testimonial_attribution']?></h5>
<?php if ($block['testimonial_image'] !== '') : ?>
<div class="testimonial-image"><img src="<?=$block['testimonial_image']['sizes']['image-90-90']?>"/></div>
<?php endif; ?>
<?php if ($block['button_url'] !== '') : ?>
<a href="<?=$block['button_url']?>" class="caps outline button" ><?=$block['button_text']?></a>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
Oddly the button stays hidden but the testimonial_image circle div doesn’t stay hidden.
I’m not sure that I completely understand what $block['testimonial_image']
is expected to hold at this point. If it is an image field that is returning an array it could hold an empty array, the value false
or an empty string it no image is entered. Instead of checking to see if it !==
exactly equal an empty string try empty
<?php if (!empty($block['testimonial_image'])) : ?>
<div class="testimonial-image"><img src="<?=$block['testimonial_image']['sizes']['image-90-90']?>"/></div>
<?php endif; ?>
Wow, thank you so much! Truly thank you for all your help! That worked perfectly!
The topic ‘FOR EACH Inside of an IF/ELSE using Select Field/Repeater’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.