Did you manage to fix this? It has just happened to me too after updating.
Hi John, again thanks for your help so far. Editing the 2 $previous_row_value
and $next_row_value
to have $prevIndex
and $nextIndex
still gives me a error of
Undefined offset: -1
for $previous_row_value = $blocks[$prevIndex-1]['fb_title'];
for the first row
and
Undefined offset: 5
for $next_row_value = $blocks[$nextIndex+1]['fb_title'];
on the last row
I’ve also noticed its now pulling in the 2nd row previous title now and not the previous row.
Hi John
Thanks for your help, nearly there!
Im trying to do the continuous loop so I can show the last on the first and first on the last.
Below is my foreach set up
<?php foreach( $blocks as $index => $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'];
// Previous row title
$prevIndex = $index - 1;
if( $prevIndex < 0 ) :
$prevIndex = count($blocks)-1;
endif;
$previous_row_value = $blocks[$index-1]['fb_title'];
// Next row title
$nextIndex = $index + 1;
if( $nextIndex >= count($block) ) :
$nextIndex = 0;
endif;
$next_row_value = $blocks[$index+1]['fb_title'];
?>
Im getting an error Undefined offset: -1 for $previous_row_value = $blocks[$index-1]['fb_title'];
and Undefined offset: 5 for $next_row_value = $blocks[$index+1]['fb_title'];
Hi John thanks a lot for you comments. Im not at my desk at the moment to test so will update later.
Ideally I’d want the first to show the last and the last to show the first.
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 @johnhuebner
Im stuck on something similar where I need to get the field of the previous and next row using the foreach. Can you give me any advice to please?
Hi John
Thanks for taking the time to help. That’s done the trick 🙂
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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.