Home Forums Add-ons Repeater Field Retrieving number value from field order?


Retrieving number value from field order?

  • I have made a “top lists” style repeater field.

    Right now, I have a number field where one can manually set the number to display on the item in the list.

    Is it possible to retrieve the row number from the repeater field? This way when the field is re-ordered it will automatically update.

    Edit: I forgot to mention this repeater field IS in flex content.

    Thank you in advance!

  • Hi @bluebirdhaze

    You can use a simple counter to output the row number dynamically within a repeater field like so:

    <?php if( have_rows('repeater') ): $i = 0; ?>
    	<ul class="slides">
    	<?php while( have_rows('repeater') ): the_row(); $i++; ?>
    			<?php echo $i ?>. <?php the_sub_field('style'); ?> 
    	<?php endwhile; ?>
    <?php endif; ?>


  • Thank you. I tried this, and it “worked” but for example in one article, there are 7 rows, on every single row it is displaying “1235467”. Any ideas?

    Below is the code without your suggestions. I learn by editing examples I have found. Please forgive me if the code isn’t exactly the best method.

    [‘list_item_number’] is where I wanted the number to appear.

    // ***************
    // List Article Item
    // *************** 
    if (get_row_layout() == "list_article"){  //Flex Field Name
    	$rows = get_sub_field('list'); //Repeater Field Name
    	echo '<div class="toplist-container flextainer">';
      if ($rows){ 
            foreach($rows as $row){ 
    	echo '<div class="toplist-box">';
            echo '<div class="toplist-heading"><span class="toplist-number ';
    	echo '">#'.$row['list_item_number'].'</span><span class="toplist-title"> '.$row['list_item_title'].'</span></div><!--/heading-->';
    	echo '<div class="'.$row['show_list_image'].'" style=" background-image: url('.$row['list_item_image'].');"></div><!--/image-->';
    	echo '<div class="toplist-text">'.$row['list_item_content'].'</div><!--/text-->';
            echo '<span class="clear-fix"></span></div><!--/toplist-box-->';
    	echo '<span class="clear-fix"></span></div><!--/flextainer-->';
  • Hi @bluebirdhaze

    Your code doesnt seem to make use of a counter variable. Have you attempted to add one in.

    Just define $i before the loop, and then within your loop increase the counter by 1 ($i++). Then just echo this out when you need it.

    Perhaps you could jump on google and research a PHP counter variable?


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

The topic ‘Retrieving number value from field order?’ is closed to new replies.