Home › Forums › Add-ons › Repeater Field › Repeater sub field display by group based on value › Reply To: Repeater sub field display by group based on value
With the help of a collegue, I rewrote the code for my purpose (and it works) :
function sortByGroup($a, $b) {
if ($a['partner-category'] > $b['partner-category']) {
return -1;
} elseif ($a['partner-category'] < $b['partner-category']) {
return 1;
}
return 0;
}
$partnerArray = get_field('partner');
if (!empty($partnerArray)) {
usort($partnerArray, 'sortByGroup');
$last_group = ''; // initialize the group name
foreach ($partnerArray as $partner) {
$this_group = $partner['partner-category'];
if ($this_group != $last_group) {
$last_group = $this_group;
echo '<h3>',$partner['partner-category'],'</h3>';
}
if ($partner['partner-website']) {
$partner_bloc = '<a href="'.$partner['partner-website'].'">'.$partner['partner-name'].'</a>';
}
echo $partner_bloc;
} // end foreach
} // end if personArray
The line $last_group = $this_group;
was missing in order to display the H3 only one time by group. And the sorting function has been inverted in order to reflect the backend order (from top to bottom).
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.