ACF Repeater display row number

Looking to display the individual number of each repeater row. So for example, the first row will display "1" and the second row will display "2".

I found this from Elliot,

<?php echo count( get_field('repeater_field') );?>

which counts how many rows there are in total. But I need the individual number next to each.

Thanks

1

3 Answers

I would suggest taking a look at Elliot's answer here:

You would want to set up a counter variable ( $i ) and then add 1 to $i inside the loop.

<?php if( have_rows('repeater_field') ): $i = 0; ?> <div> <?php while( have_rows('repeater_field') ): the_row(); $i++; ?> <p>This is row number <?php echo $i; ?>.</p> <!-- call your sub_fields as needed --> <?php endwhile; ?> </div>
<?php endif; ?>

This would output a div with a paragraph tag that displays your row number.

1

There is get_row_index() already available to use.

<?php if( have_rows('tabel_produse_profit') ): $i = 0; ?>
<table width="100%"> <tr> <td>Nr.</td> <td>Imagine</td> <td>Nume</td> <td>Evaluare</td> <td>Verificati pretul</td> </tr> <?php while( have_rows('tabel_produse_profit') ): the_row(); $i++; // vars $image = get_sub_field('tabel_imagine_produs'); $link = get_sub_field('tabel_link_profit'); $titlu = get_sub_field('tabel_titlu_profit'); $evaluare = get_sub_field('tabel_evaluare'); $count = count(get_field('tabel_produse_profit')); ?> <tr> <td> <?php echo $i; ?> <?php if( $count ): ?> <div> din <?php echo $count; ?></div> <?php endif; ?> </td> <td> <?php if( $image ): ?> <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" /> <?php endif; ?> </td> <td> <?php if( $titlu ): ?> <h2><?php echo $titlu; ?></h2> <?php endif; ?> </td> <td> <?php if( $evaluare ): ?> <div><?php echo $evaluare; ?></div> <?php endif; ?> </td> <td> <?php if( $link ): ?> <a href="<?php echo $link; ?>">VERIFICAȚI PREȚUL</a> <div>pe emag.ro</div> <?php endif; ?> </td> <?php echo $content; ?> </tr> <?php endwhile; ?> </table>
<?php endif; ?>

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like