Skip Navigation

[Resolved] Conditional output and checkboxes

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a conditional that tests whether any checkbox in a checkboxes group is checked, or whether no checkboxes in a checkboxes group are checked.

Solution:
Saving 0 to the database when checkboxes are not selected is not recommended if you plan to use this field in a custom search View. If that's not a problem for you, here is the code:

[wpv-conditional if="( $(wpcf-fieldslug) ne '0' )"]
Something is checked
[/wpv-conditional]
  
[wpv-conditional if="( $(wpcf-fieldslug) eq '0' )"]
Nothing is checked
[/wpv-conditional]
This support ticket is created 6 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 3 replies, has 2 voices.

Last updated by Christian Cox 6 years, 5 months ago.

Assisted by: Christian Cox.

Author
Posts
#1080226

Hi! I need to add a conditional code ONLY if ANY option of a checkboxes field is selected. I see how can I do it for single checkbox options but I need a way, a simple way, to add some code only if the field group is valorized. Thank you.

#1080675

Hi, you can test whether or not the value is empty like this:

[wpv-conditional if="( $(wpcf-fieldslug) ne '' )"]
Something is checked
[/wpv-conditional]

[wpv-conditional if="( $(wpcf-fieldslug) eq '' )"]
Nothing is checked
[/wpv-conditional]

Change fieldslug to match your custom checkboxes field slug. So if your checkboxes field slug is "cbs" then it should be like this:

[wpv-conditional if="( $(wpcf-cbs) ne '' )"]
#1081325

Unfortunately this is not working as wehn the checkboxes are not selected they store a '0' value in the db. So the field is never empty.

#1081408

Change the conditional to test for '0' instead of empty like this:

[wpv-conditional if="( $(wpcf-fieldslug) ne '0' )"]
Something is checked
[/wpv-conditional]
 
[wpv-conditional if="( $(wpcf-fieldslug) eq '0' )"]
Nothing is checked
[/wpv-conditional]

One quick comment - the option "save 0 to the database" is very likely to cause problems if you want to use this field in a custom search. If you want to use this checkboxes group custom field for a custom search filter, I recommend you reconsider this option.