Support

Account

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

Solving

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!

    • Elliot

    • December 13, 2013 at 10:37 am

    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++; ?>
     
    		<li>
    			<?php echo $i ?>. <?php the_sub_field('style'); ?> 
    		</li>
     
    	<?php endwhile; ?>
     
    	</ul>
     
    <?php endif; ?>
    

    Thanks
    E

  • 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">';
    //REPEATER START
      if ($rows){ 
            foreach($rows as $row){ 
    	echo '<div class="toplist-box">';
            echo '<div class="toplist-heading"><span class="toplist-number ';
    	the_sub_field("numbers_switch_1");
    	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-->';
    	}
          } 
    //REPEATER END
    	echo '<span class="clear-fix"></span></div><!--/flextainer-->';
     }
    • Elliot

    • December 15, 2013 at 5:51 pm

    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?

    Thanks
    E

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

You must be logged in to reply to this topic.

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.