Home › Forums › Add-ons › Repeater Field › Conditional Statements Using Repeater: if… else
Sorry for my english and my php…
Inside a repeater, I want to insert either an image or video.
I tried with no success to add a conditional action in my code, but it did not work. I would like to insert :
if: image_service then image
if image is empty, then insert the video.
I hope It clears enough. Behind is my code…
<?php
$rows = get_field('bloc_service_internet');
if($rows)
{
foreach($rows as $row)
{
echo '<div class="box gris-clair"><div class="twocol-one"><h3>' . $row['titre_service_internet'] . '</h3><p>' . $row['texte_service_internet'] . '</p><div class="box contact">' . $row['bloc_en_savoir_plus'] . '</div></div><div class="twocol-one last">';
if( $row['image_service_internet']) echo '<img src="' . $row['image_service_internet'] .'"></div><div class="fix"></div></div><div class="woo-sc-divider"></div>';
}
}
?>
Thanks for your help!
Hi @gd6d
No worries. Your code would look something like this:
<?php
$rows = get_field('bloc_service_internet');
if($rows)
{
foreach($rows as $row)
{
echo '<div class="box gris-clair"><div class="twocol-one"><h3>' . $row['titre_service_internet'] . '</h3><p>' . $row['texte_service_internet'] . '</p><div class="box contact">' . $row['bloc_en_savoir_plus'] . '</div></div><div class="twocol-one last">';
if( $row['image_service_internet'])
{
echo '<img src="' . $row['image_service_internet'] .'"></div><div class="fix"></div></div><div class="woo-sc-divider"></div>';
}
else
{
// echo video
}
}
}
?>
This is exactly what I was needing help with, perhaps you could give me a hand with my issue.
Your example works great but what I am trying to do is echo a repeater field within the foreach, where $row[‘ingredients’] would be my parent field and at the moment it simply kicks back array
Any ideas on how I could make this work?
I would not use the foreach stlye loop as demonstrated above. Instead, I would use the nested friendly has_fields or have_rows while loop functions.
You will find nested code examples in the docs. Please do check them out on the docs
Thanks
E
The topic ‘Conditional Statements Using Repeater: if… else’ 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.