Support

Account

Home Forums Front-end Issues Checking whether a checkbox is checked

Solved

Checking whether a checkbox is checked

  • I’m struggling with ACF checkboxes, I’m trying to check the array to see if ‘car’ has been ticked:

    if( in_array( 'car', the_sub_field('tyres_available') ) )
    {
        echo 'some html';
    }

    The checkbox is in a repeater field. ‘car’ is one of the checkbox options. If it’s ticked I want to echo out some html. At the moment it’s outputting the whole array for every checkbox that has been ticked in the field ‘tyres_available’.

    Choices in my field ‘tyres_available’ are:
    car : Car
    truck : Truck
    4×4 : 4×4
    Agri : Agricultural
    forkLift : Fork lift
    bike : Motorbike
    caravan : Caravan

    Any ideas where am I going wrong?

    Thanks

  • Hi @geoffmuskett

    Change the_sub_field() method and use get_sub_field() instead. the_sub_field() method will display the value whereas get_sub_field() method will get the value. Change your code to the following:

    if( in_array( 'car', get_sub_field('tyres_available') ) ) {
        echo 'some html';
    }

    Check out the docs for more information: http://www.advancedcustomfields.com/resources/get_sub_field/

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

The topic ‘Checking whether a checkbox is checked’ is closed to new replies.