Support

Account

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).