Hello,
-
I have created a repeater field on my options page.
The sub fields are… Name (text field) and URL (text field)
-
I have then successfully managed to populate a checkbox field in a separate Field Group with the options page repeater data. This is done by adding code into functions.php (see code below).
-
I now want to display the Label and the Value for each checkbox ticked in the Field Group with in my theme. It seems I can only output the Value and not the Label. I noticed that only the “Choices” setting was filled in and the “Default Value” was not filled with corresponding information. See screen shot.
- My question is…
Is my functions.php code wrong and how do I output both the value and label of the Checkboxes in my template?
function.php code:
// Add Technology Page Options to Technology Field Group
function my_acf_load_field( $field )
{
// reset choices
$field['choices'] = array();
// load repeater from the options page
if(get_field('technology', 'option'))
{
// loop through the repeater and use the sub fields "value" and "label"
while(has_sub_field('technology', 'option'))
{
$value = get_sub_field('technology_page_link');
$label = get_sub_field('technology_name');
$field['choices'][ $value ] = $label;
}
}
// Important: return the field
return $field;
}
// v3.5.8.2 and below
add_filter('acf_load_field-report_technology_pages', 'my_acf_load_field');
// v4.0.0 and above
add_filter('acf/load_field/name=report_technology_pages', 'my_acf_load_field');
For anyone having similar issues here is the solution…
<?php
$field = get_field_object('report_technology_pages');
$technologies = get_field('report_technology_pages'); // array of selected technology values ?>
<ul>
<?php foreach($technologies as $technology){ ?>
<li><a href="<?php echo $technology; ?>"><?php echo $field['choices'][ $technology ]; ?></a></li>
<?php } ?>
</ul>
</div>
I was having the illegal offset type error when trying to get labels using the method documented in Resources, and your solution fixed it. Thanks!