Support

Account

Home Forums Add-ons Repeater Field if/else within a sorted repeater field

Solved

if/else within a sorted repeater field

    • marenne

    • October 1, 2013 at 12:17 am

    I am trying to display a different output when a repeater field doesn’t have an image (afbeelding) uploaded to it. My code below just outputs everything twice. Please, can you tell me what I am doing wrong?

    <?php $repeater = get_field('advertenties');
      
      foreach( $repeater as $key => $row )
    	{
        	$column_id[ $key ] = $row['datum'];
    	}
    
    	array_multisort( $column_id, SORT_DESC, $repeater );
    	?>
      	<div id="advertenties">
        	<?php
    		
    		foreach( $repeater as $row )
    		{
    	
    			$attachment_id = $row['afbeelding'];
    			$image = wp_get_attachment_image_src( $attachment_id, "thumbnail" );
    
    			// url = $image[0];
    			// width = $image[1];
    			// height = $image[2];
    
    			if($row['afbeelding'])
    			{
    				echo '<div id="contentwrapper" style="clear:both;">
    				<div class="nine columns" role="main">';
    				echo '<b>' . $row['datum'] . ' - ' . ucfirst($row['type']) . '</b><Br>
    				<p>' . ucfirst($row['tekst']) .'</p></div>';
    				echo '<aside class="three columns" id="afbeeldingssidebar">
    				<img src="' . $image[0] . '" width="' . $image[1] .'" height="'. $image[2]. '" /></aside>
    				<br class="clear" /></div>';
    			}
    			else;
    			{
        			echo 'something else';
    			}
    		}?>
     	</div>
    • Elliot

    • October 1, 2013 at 3:26 pm

    Hi @marenne

    You can use an if / else like so:

    
    if( $row['afbeelding'] )
    {
        // image does exist
    }
    else
    {
        // image does no exist
    }
    
    • marenne

    • October 1, 2013 at 4:23 pm

    Yes, that is what I did, but it seems like its never empty (and returns both if and else). Maybe because of the multisort? I just can’t figure it out…

    • Elliot

    • October 1, 2013 at 5:22 pm

    Hi @marenne

    an if / else can’t run both. You must be seeing another issue. Perhaps you need to do some debugging? Print out variables and see visually what is happening in your code line by line.

    Thanks
    E

    • marenne

    • October 2, 2013 at 1:12 am

    Thanks. It seems like I put my if statement in the wrong place!

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.