Skip Navigation

[Resolved] Conditional output based on the number of checkboxes option selected

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by joseM-8 1 year, 4 months ago.

Assisted by: Waqar.

Author
Posts
#2501967

Tell us what you are trying to do?

I want to able to display a message only it more than on option is selected from a checkbox group

Is there any documentation that you are following?
I can't really find any

Is there a similar example that we can see?
no

What is the link to your site?

#2502131

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

To achieve this, you'll first need to register a custom shortcode, which can return the number of the checked options from a 'checkboxes' type custom field.


add_shortcode( 'show_count_checkboxes_field', 'show_count_checkboxes_field_func');
function show_count_checkboxes_field_func( $atts ) {
    $atts = shortcode_atts( array(
        'id' => '',
        'field' => '',
    ), $atts );
 
    // slug of the field
    $field_slug = $atts['field'];
 
    // getting the field's output separated by,
    $field_output = types_render_field( $field_slug, array( 'item' => $atts['id'], 'separator' => ',' ) );
 
    // if there are some values returned, explode the string into an array and return the count
    if(!empty($field_output)) {
        $field_output_arr = explode(',', $field_output);
        return count($field_output_arr);        
    }
    else
    {
        // if no checked field values are returned, return 0
        return 0;
    }
}

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please add "show_count_checkboxes_field" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

This new shortcode will accept two attributes, 'id' and the 'field', for example:


[show_count_checkboxes_field id='[wpv-post-id]' field='checkboxes-field-slug']

The above shortcode will return the number of the checked options from the field with the slug 'checkboxes-field-slug', from the current post ( as the 'id' is passed using the custom post's ID shortcode [wpv-post-id] ).

And the same shortcode can be used in a conditional statement to check for more than one checked option, like this:


[wpv-conditional if="( '[show_count_checkboxes_field id='[wpv-post-id]' field='checkboxes-field-slug']' gte '2' )"]
Two or more options checked!
[/wpv-conditional]

I hope this helps and please let me know if you need any further assistance with this.

regards,
Waqar

#2505585

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.