Support

Account

Home Forums Front-end Issues Hiding link if empty in a repeater row

Solved

Hiding link if empty in a repeater row

  • Hello, I managed to randomise a bunch of rows from a repeater and also link them to their specific link fields in the panel. I’m simply wondering how can I make the items to do not have link in case the field is empty.

    Right now it links to the homepage if there’s no link:

    <?php
    $rows = get_field('home' ); //get all rows
    shuffle ($rows); //randomize rows
    if($rows) {
    	foreach($rows as $row) { //print rows ?>
    		<div class="home-box">
    			<?php
    			echo '<a href="';
    			echo $row['link'];
    			echo '">';
    			echo $row['text'];
    			echo '</a>'
    			?>
    	</div>
    	<?php
    	}
    }
    
    ?>
  • 
    <?php
    $rows = get_field('home' ); //get all rows
    shuffle ($rows); //randomize rows
    if($rows) {
    	foreach($rows as $row) { //print rows 
    if (!empty($row['link'])) {
    
    ?>
    		<div class="home-box">
    			<?php
    			echo '<a href="';
    			echo $row['link'];
    			echo '">';
    			echo $row['text'];
    			echo '</a>'
    			?>
    	</div>
    	<?php
    } // end if link !empty
    	}
    }
    
    ?>
    
  • Sorry, I perhaps didn’t explain my problem properly.
    I want the content to be always shown, but remove the link if the link field is empty.

  • Then I would probably do something like

    
    $text = $row['text'];
    if (!empty($row['link'])) {
      $text = '<a href="'.$row['link'].'">'.$text.'</a>';
    }
    echo $text;
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.