Support

Account

Home Forums Front-end Issues Adding Separators to Select-Field Options

Solving

Adding Separators to Select-Field Options

  • I want to add separators to the select field to group different sorts of options and make a large number of options a bit more orderly.

    I.e.:

    – Choose your favourite food –
    Apple
    Banana
    Grapes
    Oranges
    ———
    Cheese
    Yoghurt
    ———
    Bread
    Pasta
    Rice

    My first impulse would be to add the separators as options and disable them with JavaScript. But I’m guessing that I’m not the first person who tries to do this, so I was wondering if there was some simpler way to implement it.

  • Hi @dtx211

    I believe your approach is the best way to do it. But please keep in mind that ACF won’t allow you to have multiple choices with the same value.

    You can also group the choices by modifying them using the acf/load_field hook. It should be something like this:

    function acf_load_color_field_choices( $field ) {
        
        // grouped choices
        $field['choices']['Fruit'] = array('Apple', 'Banana', 'Grapes', 'Oranges');
        $field['choices']['Dairy'] = array('Cheese', 'Yoghurt');
    
        // return the field
        return $field;
        
    }
    
    add_filter('acf/load_field/name=select', 'acf_load_color_field_choices');

    I hope this helps 🙂

  • It helps immensely. Bruce, kindly send three bars of chocolate to the man in the third row.

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

The topic ‘Adding Separators to Select-Field Options’ is closed to new replies.