
I am trying to display multiple select values on the frontend.
This code currently works when the multi-select option is turned off, and correctly display images for one field that is selected without issue. However, nothing works for multi fields when I turn the option on (no matter how many are selected).
Can anyone spot what’s missing?
functions.php:
function acf_load_college($field)
{
$field['choices'] = array();
if (have_rows('gallery_repeater', 'option')) {
while (have_rows('gallery_repeater', 'option')) {
the_row();
$value = get_row_index();
$label = get_sub_field('college');
$field['choices'][$value] = $label;
}
}
return $field;
}
add_filter('acf/load_field/name=choices', 'acf_load_college');
add_filter('acf/settings/row_index_offset', '__return_zero');
Displaying on frontend:
<?php
$field = get_field_object('choices');
$value = $field['value'];
$rows = get_field('gallery_repeater', 'option');
$specific_row = $rows[$value]; //dumping this returns null when multi-select option is turned on, but returns correct repeater array for selected value when multi-select is off
$sub_field_value = $specific_row['gallery'];
?>
<?php foreach ($sub_field_value as $img) : ?>
<div data-src="<?php echo esc_url($img['sizes']['large']); ?>" title="<?php echo esc_attr($img['title']); ?>">
<img src="<?php echo esc_url($img['sizes']['large']); ?>" alt="<?php echo esc_attr($img['alt']); ?>" title="<?php echo esc_attr($img['title']); ?>" />
</div>
<?php endforeach ?>
</div>
</div>
For anyone interested, or has had the same issue, I solved this myself. Value to become values and the singular value to loop through each array item:
<?php
$field = get_field_object('choices');
$values = $field['value'];
foreach ($values as value) :
$rows = get_field('gallery_repeater', 'option');
$specific_row = $rows[$value];
$sub_field_value = $specific_row['gallery'];
?>
<?php foreach ($sub_field_value as $img) : ?>
<div data-src="<?php echo esc_url($img['sizes']['large']); ?>" title="<?php echo esc_attr($img['title']); ?>">
<img />" alt="<?php echo esc_attr($img['alt']); ?>" title="<?php echo esc_attr($img['title']); ?>" />
</div>
<?php endforeach ?> // $img
</div>
<?php endforeach ?> // $value