Support

Account

Home Forums Add-ons Repeater Field Conditional Statements Using Repeater: if… else

Solved

Conditional Statements Using Repeater: if… else

    • gd6d

    • August 14, 2013 at 10:00 am

    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!

    • Elliot

    • August 14, 2013 at 2:35 pm

    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
    		}
    	}
    }
     
    ?>
    
    • gd6d

    • August 14, 2013 at 6:32 pm

    Thanks, it works fine!
    Great plugin, great support…

  • 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?

    • Elliot

    • November 9, 2013 at 10:12 am

    Hi @thestrengster

    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

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.