Support

Account

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

Solved

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

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

The topic ‘Conditional Statements Using Repeater: if… else’ is closed to new replies.