I'm using the ACF Repeater to display a list of team members on a company's website, grouped by a select field called "Group". I was able to get the behavior I wanted but I'm wondering if there's a better way to do it.
<?php
function displayTeamMember() {
echo '<div class="row-fluid team-member">';
if(get_sub_field('photo')) {
echo '' . wp_get_attachment_image( get_sub_field('photo'), "medium", 0, 'class=img-polaroid') . '';
echo '';
} else {
echo '';
}
echo '' . get_sub_field('name') . '';
echo '' . get_sub_field('title') . '';
echo '' . get_sub_field('bio') . '';
echo '';
echo '';
}
function makeCSSClass($string) {
//lower case everything
$string = strtolower($string);
//make alphaunermic
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
// Get list of groups
if( get_field('team_member') ){
if ( has_sub_field('team_member') ){
$select = get_sub_field_object('group');
}
while(has_sub_field('team_member')){
// do nothing in this pass
// resets the loop for the code below
}
}
// Display team members by group
if( get_field('team_member') ){
foreach ($select['choices'] as $k => $v){
echo '';
echo '' . $v . '';
while( has_sub_field('team_member') ) {
if(get_sub_field('group') == $v) {
displayTeamMember();
}
}
echo '';
}
}
?>
The part that originally threw me off was when I used has_sub_field('team_member') to first get the values of the select field, the first item was missing when I used it again to start the while loop.
I added an additional while loop in that initial code that doesn't do anything except run through the loop so that it's reset for the next time has_sub_field() is called.
Is there a better way to do this or am I just trying to do something that the repeter wasn't designed to do?