You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.

First: Double-Check Your Repeater Loop Structure

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;
Key Settings & Troubleshooting Checks

Let’s rule out the other usual suspects:

  1. 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. Use var_dump($contact_groups) to see what format you’re working with.
  2. 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 is contact_entries instead of contacts, that’s why nothing’s showing up.

  3. 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 to have_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;
    
  4. Empty Checkbox Handling
    If a contact row has no checkboxes selected, get_sub_field('groups') will return false or an empty array. Always wrap your value handling in an if ($contact_groups) check to avoid PHP warnings.

Final Debugging Trick

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:35:46