danrodrigues
danrodrigues

Reputation: 181

Display Selected as well as NOT Selected checkboxes using ACF

I tried to find a related topic on the acf site but that didn't give me the right answer. Hopefully you guys can help me out.

I am building a custom website using ACF, everything is perfect and I managed to get almost everything exactly the way I want but with the checkboxes I can only display the checked ones but not the unchecked.

My idea is that the not checked ones would have a different "class" or "id" and I would display them with a different CSS style.

This is a list of features of the flats for rent, so things like: Internet, TV, etc. Much like airbnb's website.

So the code. I managed to get this code to work:

<?php

    $values = get_field('room_features');
    if($values)
    {
        echo '<ul>';

        foreach($values as $value)
        {
            echo '<li>' . $value . '</li>';
        }

        echo '</ul>';
    }

    ?>

But this doesn't display the unchecked ones, only checked.

I would appretiate any light on this :)

Upvotes: 2

Views: 2895

Answers (2)

Manish
Manish

Reputation: 1

You can use following code.

$field = get_field_object('field_name');

You will get the object which contains the all choices of checkbox.

Upvotes: 0

johnnyd23
johnnyd23

Reputation: 1705

You can return an array of all data associated with a certain field using the get_field_object() function: http://www.advancedcustomfields.com/resources/get_field_object/

Here's an example of the returned array on a checkbox field. Note the "choices" array.

Array
(
    [key] => field_551556a8ce42d
    [label] => Test
    [name] => test
    [_name] => test
    [type] => checkbox
    [order_no] => 9
    [instructions] => 
    [required] => 0
    [id] => acf-field-test
    [class] => checkbox
    [conditional_logic] => Array
        (
            [status] => 0
            [rules] => Array
                (
                    [0] => Array
                        (
                            [field] => null
                            [operator] => ==
                        )

                )

            [allorany] => all
        )

    [choices] => Array
        (
            [option 1] => option 1
            [option 2] => option 2
        )

    [default_value] => 
    [layout] => vertical
    [field_group] => 19
    [value] => 
)

Upvotes: 1

Related Questions