Support

Account

Home Forums Add-ons Repeater Field close if subfield has no value

Solving

close if subfield has no value

  • Hi there!

    I wrote the code below to loop trough a repeater field called “contentblock” with 4 subfields. The problem is, that if one of the subfields is empty, the containing <div> is not closed and thus the repeating content blocks get nested into each other, causing multiple layout problems.
    So, how can I close the containing div, e.g. the image gallery (“repeaterimages”), if no images are chosen?


    function my_repeater() {
    // check if the repeater field has rows of data
    if( have_rows(‘repeater_field’) ):

    // loop through the rows of data
    while ( have_rows(‘repeater_field’) ) : the_row();

    // display a sub field value?>
    <div class=”contentblock”>

    <?php $ankerlink = get_sub_field(‘_repeater_anker’); ?>
    <div style=”position: relative;”>
    <?php if( $ankerlink ): ?>
    <div id=”<?php echo $ankerlink ?>” class=”anchorposition”></div>
    </div>
    <?php endif; ?>

    <h3>
    <?php the_sub_field(‘_repeater_headline’); ?>
    </h3>

    <div class =”repeaterimages”>
    <?php $images = get_sub_field(‘_repeater_bild’);
    if( $images ):?>
    <?php foreach( $images as $image ): ?>
    <div class=”imageblock fitimagecontainer”>” alt=”<?php echo $image[‘alt’]; ?>” /></div>
    <?php endforeach; ?>
    </div>

    <div class=”textblock”>
    <?php the_sub_field(‘_repeater_text’); ?>
    </div>

    </div>

    <?php endif;
    endwhile;

    else :
    // no rows found
    endif;}

  • You’ll need to repost your code using code tags. Portions of you code have been stripped because of this and it’s impossible to follow.

  • like this??

    <?php
    function my_repeater() { 
    
    if( have_rows('repeater_field') ):
    
     	
        while ( have_rows('repeater_field') ) : the_row(); 
    
            
        <div class="contentblock">
            
            <?php $ankerlink = get_sub_field('_repeater_anker'); ?>
            <div style="position: relative;">	 	 
    		<?php if( $ankerlink ): ?>
    		<div id="<?php echo $ankerlink ?>" class="anchorposition"></div>	 	 
    		</div>
    		<?php endif; ?>
            
            <h3>
            <?php the_sub_field('_repeater_headline'); ?>
            </h3>
           
           <div class ="repeaterimages">
           <?php $images = get_sub_field('_repeater_bild'); 
    			if( $images ):?>
           		 <?php foreach( $images as $image ): ?>
    	      	<div class="imageblock fitimagecontainer">
    <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" /></div>
              	<?php endforeach; ?>
               	</div>
        	          
             <div class="textblock">
              <?php the_sub_field('_repeater_text'); ?>
              </div>
             
        </div>
            
    	<?php endif; 
       endwhile; 
       
    else :
    // no rows found
    endif;}
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.