Home › Forums › Add-ons › Repeater Field › Get value from next row in repeater › Reply To: Get value from next row in repeater
Getting the previous and next row would depend on what you want to do at the ends of the row. For example, if the current row is the first row of the repeater do you want to show nothing for previous or do you want to show the last row?
The first think you need to know in your loop is the current index, you do that by adjusting the loop
foreach( $blocks as $index => $block ) :
Next it depends on what you want to do. To show nothing before and after at the beginning/end
// previous row
if ($index > 0) {
$previous_row_value = $blocks[$index-1]['field_name'];
}
// next row
if ($index+1 < count($blocks)) {
$next_row_value = $blocks[$index+1]['field_name'];
}
if you want to have a continuous loop then you need to calculate the next and previous index
$prev_index = $index - 1;
if ($prev_index < 0) {
$prev_index = count($blocks)-1;
}
$next_index = $index + 1;
if ($next_index >= count($blocks)) {
$next_index = 0;
}
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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
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.