Support

Account

Home Forums Front-end Issues Checkbox trouble

Solving

Checkbox trouble

  • Good morning,

    I would like to pull the value and label for my checkboxes created with ACF. Here is a screenshot of what the checkbox looks like when building the page.

    http://prntscr.com/7lx35n

    When I go to use it in my content-page.php file I first tried using get_sub_field, but it returned an array of just the values without their labels. After this I researched on the ACF site and found this post about checkboxes.

    http://www.advancedcustomfields.com/resources/checkbox/

    The post suggests using the_field, get_field_object and get_field but every time I try those it returns null or false. I also read a post in the forum suggesting get_sub_field_object and that too returned null or false.

    So is the documentation outdated or am I just using it incorrectly?

  • When you tried get_field_object() or get_sub_field_object() did you use the field name or the field key.

    In order to tell if you’re using it wrong I’ll need to see some code. Can you post the code of your has_sub_field() loop for the repeater with the code you’re using to get the sub field object?

  • $fields = get_sub_field(‘of_content_address_fields’);

    That is my current code. It does get an array of the values but I was hoping for the keys too.

  • I was speaking more about the repeater loop and your attempt at using get_sub_field_object() so that I might see what you are doing wrong.

    This is a simple loop that will get a sub field object and output that object to the page so you can see what you’re working with.

    
    if (get_field('repeater')) {
        while (has_sub_field('repeater')) {
            echo '<pre>'; print_r(get_sub_field_object('checkbox')); echo '</pre>';
        }
    }
    

    The output of this looks something like this:

    
    Array
    (
        [ID] => 545
        [key] => field_558ef367f2c80
        [label] => checkbox
        [name] => checkbox
        [prefix] => 
        [type] => checkbox
        [value] => Array
            (
                [0] => choice1
                [1] => choice2
            )
    
        [menu_order] => 0
        [instructions] => 
        [required] => 0
        [id] => 
        [class] => 
        [conditional_logic] => 0
        [parent] => 544
        [wrapper] => Array
            (
                [width] => 
                [class] => 
                [id] => 
            )
    
        [_name] => checkbox
        [_input] => 
        [_valid] => 1
        [choices] => Array
            (
                [choice1] => Choice 1
                [choice2] => Choice 2
                [choice3] => Choice 3
                [choice4] => Choice 4
            )
    
        [default_value] => Array
            (
                [] => 
            )
    
        [layout] => vertical
        [toggle] => 0
    )
    

    There area that you’ll get your labels from is in the index [choices]. You’ll need to loop through that array and compare the values in the returned values array with the values these values to get the labels.

    Hope that helps.

    ~JH

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.