Home › Forums › Add-ons › Repeater Field › How to get the count of a child field inside a nested repeater
Hello all,
I have a page that has three levels of nested repeaters, which are structured like so:
* Field Structure:
* - grandparent_repeater (Repeater)
* - grandparent_building (number)
* - parent_repeater (Repeater)
* - parent_floor (number)
* - child_repeater (Repeater)
* - availability (text)
* - apartment_number (number)
* - price (number)
* - rooms (number)
*/
It’ all working, except what i need to do now which is get the number of apartments (using the apartment_number
, since all apartments have a number) on each floor, $parent_floor
. Right now, the code as I am pasting in gives me an error because $count
is not used on an array.
How can i make that work? I have a little more experience writing jQuery then PHP. In the code below is the line that is not working, $count = count(get_sub_field('$apartment_number'));
. Also FYI, you don’t see the grandparent repeater as it doesn’t affect this question.
If it helps, what i am doing with this is a page listing apartments. It displays all the floors of a building, and each floor is a row (except for mobile screens) with the number of columns equal to the number of floors. You can see below echo '<ul class="apt-row inFade apt-' . $count . '">';
which is where I want to use the results of getting the number of $apartment_number
for each floor to appand to apt-
that number: <ul class="apt-row inFade apt-">
.
Floors with 5 units, their ul
will have a class of apt-5
, floors with 4 units will have apt-4
, etc. The reason for all this is the building has a penthouse, with 1 unit, and a 2nd penthouse on the next-to-top floor with 2 units. So this design helps show the apartments in a visual layout that matches how nice it is.
// Loop over sub repeater rows.
if( have_rows('parent_repeater') ):
while( have_rows('parent_repeater') ) : the_row();
echo '<ul class="floor tower' . $grandparent_building . '">';
// Get parent value.
$parent_floor = get_sub_field('parent_floor');
echo '<li class="apHeight sec-bg parent-floor-">';
echo '<span>Floor ';
echo $parent_floor;
echo '</span></li>';
echo '<li>';
// Loop over child repeater rows.
if( have_rows('child_repeater') ):
// Get sub values.
$availability = get_sub_field('availability');
$apartment_number = get_sub_field('apartment_number');
//// @todo : this is where i need help
// get number of apartments inside child_repeater
$count = count(get_sub_field('$apartment_number'));
// class apt- should be apt-{number of units on that floor}
echo '<ul class="apt-row inFade apt-' . $count . '">';
while( have_rows('child_repeater') ) : the_row();
// if ('ul.apt-row > li').length == '5') { echo jQuery('ul.apt-row > li').length }
echo '<li class="child-unit-">';
echo '<span class="unit-">';
echo $apartment_number;
echo '</span>';
echo '<span class="availability-unit">';
echo $availability;
echo '</span>';
echo '</li>';
endwhile;
echo '</ul>';
endif;
echo '</li>';
echo '</ul>';
endwhile;
endif;
To get the number of rows you need to get the count of the repeater instead of the count of a sub field of that repeater. You need to get the count in the parent before your call have_rows
for the parent. The reason for this is that ‘child_repeater’ is not a sub field of child_repeater
while( have_rows('parent_repeater') ) : the_row();
// ...
$count = count(get_sub_field('child_repeater'));
if( have_rows('child_repeater') ):
While I have you, can you help me sort the floors so highest is first? It is this echo '<ul class="floor tower' . $grandparent_building . '">';
which is the floors. You can see the page here: https://briga.co.il/top-penthouse/briga-blue-bay/choose-apartment-simple/
Thanks!
sorting a repeater field is not an easy thing to do, even more so with multiple nested repeaters. You’d have to somehow do it as an array and loose all ability to use have_rows
loops. I don’t even know if it’s possible.
There is a basic idea here https://www.advancedcustomfields.com/resources/how-to-sorting-a-repeater-field/
I have also used usort, but never for anything with nested values so I can’t even say if that’s possible.
You must be logged in to reply to this topic.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.