Support

Account

Home Forums Add-ons Repeater Field Previous and next field in repeater

Solving

Previous and next field in repeater

  • Hi

    Im trying to get a field for the previous and next row of a repeater using the foreach method. Can anyone please advise me on the best way to do this or point me in the right direction.

    Appreciate any help, thanks.

  • Hi

    You can try something like this with the index when you are doing the foreach loop:

    
    <?php
    
    $repeater = get_field('repeater_name');
    $repeaterCount = count($repeater);
    
    foreach ($repeater as $index => $row) {
        // if it's not the first row, that means we have a previous row
        $previousRow = $index == 0? null : $repeater[$index - 1];
    
        // if it's not the last row, that means we have a next row
        $nextRow     = $index == ($repeaterCount - 1)? null : $repeater[$index + 1];
    
        ... code
    }
    

    Cheers

  • Hi @gummi thanks for taking the time to help. I still don’t quite understand 100%. My code so far is below.

    Ideally, Im looking to pull in the previous and next title field in my loop, a long with the current loop title.

    <?php
    
    $blocks = get_field('fbs');
    
    if( $blocks ) :
    
    $count = 1;
    
    ?>
    
    <div id="fullpage">
    
    	<?php foreach( $blocks as $block ) :
    
    	// ACF Vars
    
    	$image	= $block['fb_main_image'];
    	$imagePos = $block['fb_main_image_fp'];
    
    	$title 		= $block['fb_title'];
    	$text		= $block['fb_text'];
    
    	$link 		= $block['fb_link'];
    	$linkText   = $block['fb_link_text'];
    
    	?>
    
    	<div class="section" data-anchor="section-<?php echo $count; ?>" style="background-image: url(<?php echo $image['url']; ?>);">
    
    		<section class="o-block">
    
    			<div class="o-container o-container--top">
    				<h2 class="o-type-60 u-mb-x4 u-colour-white">GET PREVIOUS TITLE HERE</h2>
    			</div>
    
    			<div class="o-container">
    
    			<h2 class="o-type-100 u-mb-x4 u-colour-white"><?php echo $title; // Current Title?></h2>
    
    			</div>
    
    <div class="o-container">
    
    			<h2 class="o-type-100 u-mb-x4 u-colour-white">GET NEXT TITLE</h2>
    
    			</div>
    </section>
    						<!-- .o-block -->
    
    					</div>
    					<!-- .section -->
    
    					<?php $count++; endforeach; ?>
    
    				</div>
    				<!-- #fullpage -->
    
    				<?php endif; ?>
  • Hi,

    if you look at the code i posted, you’d need to change your code as following:

    1. store the total repeater count before you run the foreach loop:
    $blocksCount = count($blocks);

    2. add the index when you are doing the foreach:
    <?php foreach( $blocks as $index => $block ) :

    3. your next and previous row will be the following:
    $previousRow = $index == 0? null : $blocks[$index - 1];
    $nextRow = $index == ($blocksCount - 1)? null : $blocks[$index + 1];

    4. now you can get the title of the previous and next like so:
    $previousTitle = $previousRow['fb_title'];
    $nextTitle = $nextRow['fb_title'];

    Cheers.

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

The topic ‘Previous and next field in repeater’ is closed to new replies.