WordPress/Timber/Twig技术问题:如何输出ACF repeater中的复选框值
Hey there! Let's work through why you're struggling to pull values from that checkbox field inside your ACF Repeater. I’ve dealt with this exact scenario dozens of times, so let’s break it down step by step.
The most common mistake here is not using ACF’s repeater-specific functions correctly. You can’t use get_field() for sub-fields inside a repeater—you need to wrap everything in a have_rows() loop, and use get_sub_field() to access nested fields. Here’s a reliable base template to start with:
// First, verify the repeater has rows of data if (have_rows('contacts')) : // Loop through each "contact" row while (have_rows('contacts')) : the_row(); // Grab the checkbox field for this specific contact $contact_groups = get_sub_field('groups'); // Debug tip: Dump the raw value to see what you're actually getting // var_dump($contact_groups); // Uncomment this to check the data structure // Handle the checkbox values based on ACF's return setting if ($contact_groups) : // If ACF is set to return an array (default behavior) echo '<div class="contact-groups">'; echo '<h4>Groups for this contact:</h4>'; echo '<ul>'; foreach ($contact_groups as $group) : echo '<li>' . esc_html($group) . '</li>'; endforeach; echo '</ul>'; echo '</div>'; endif; endwhile; else : // No repeater rows found echo '<p>No contacts available.</p>'; endif;
Let’s rule out the other usual suspects:
ACF Checkbox Return Value
ACF’s checkbox field has a critical setting called Return Value (found in the field’s edit screen). It can return:- An array of selected values (default)
- An array of selected labels
- An associative array (value => label)
If your code expects an array but the setting is set to something else, you’ll get unexpected results. Usevar_dump($contact_groups)to see what format you’re working with.
Field Key Spelling
Make sure the field names you’re using ('contacts'and'groups') match the field keys in ACF, not just the friendly labels. Field keys are case-sensitive—if your repeater’s field key iscontact_entriesinstead ofcontacts, that’s why nothing’s showing up.Context Matters
If you’re using this code outside a single post/page template (like a taxonomy archive or custom post type archive), you need to pass the correct context tohave_rows(). For example, on a category page:$term_id = get_queried_object_id(); if (have_rows('contacts', 'term_' . $term_id)) : while (have_rows('contacts', 'term_' . $term_id)) : the_row(); $contact_groups = get_sub_field('groups'); // Rest of your code endwhile; endif;Empty Checkbox Handling
If a contact row has no checkboxes selected,get_sub_field('groups')will returnfalseor an empty array. Always wrap your value handling in anif ($contact_groups)check to avoid PHP warnings.
If you’re still stuck, uncomment the var_dump($contact_groups) line in the first code block. This will print exactly what ACF is returning for that field. If it’s NULL or false, either the field name is wrong, the repeater row has no data, or you’re in the wrong context.
Give these steps a shot—9 times out of 10, it’s one of these simple fixes!
内容的提问来源于stack exchange,提问作者wickywills

