简体   繁体   中英

ACF Pro Repeater - Row Index and Total Count with Leading Zero

Q&A

Here's my Q&A post for ACF Pro's repeater field and how to output the current row count as well as the total overall count, with, and without a leading zero (eg 1, 2, 3 vs 01, 02, 03).

Issue 1

Using ACF Pro's repeater field, and the ACF get_row_index function I output a number for each item within the repeater field onto the page. (eg 1, 2, 3, 4...) I did this like so:

<p><?php echo get_row_index(); ?></p>

I then realised the design called for a leading zero (0) in front of the number returned by the get_row_index function if that number is less than or equal to 9 (eg 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11..).

See SOLUTION 1 below for the answer.


Issue 2

Next I wanted to output the total row count, which I did like this:
(It's important that the count function is outside of your while loop)

$number_of_items = count($myRepeaterField);

<p><?php echo $number_of_items ?></p>

I again, then noticed the design called for a leading zero for the total count too.

See SOLUTION 2 below for the answer.

Solution 1

To output a leading zero in front of the get_row_index function, you can do the following:

<p><?php echo str_pad(strval(get_row_index()), 2, '0', STR_PAD_LEFT); ?></p>

Solution 2

To output a leading zero in front of the total count of items in the repeater field:

<p><?php if ($number_of_items < 9){echo('0');} ?><?php echo $number_of_items ?></p>

I hope this information helps someone, as it took a while to discover through various google searches and forum hunting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM