I have checkboxes field in one of my post - languages. I use 'en' for English and 'de' for German.
How could I use it in conditional output?
So, let's say if I want to check it for value 'en' and then output "<p>English</p>" and similar for 'de' as "<p>German</p>"?
This is documentation about "radio" field. I have multiple checkbox.
If I compare it to exact value, it shows only one option. Let's say, if it is both "de" and "en" and I compare it to "en" it is return "false" as it is not equal "en" but both "de" and "en".
So, this is question how to check that "en" is one of values of this field?
It is quite surprise due to very obvious usage... also it could be done by string compare as "if Field include 'value'", not as it now "if Field equal to 'value'"...
Do you have any documentation about "custom conditional would need to be written to resolve this." - what is it about and how it could be done?
also it could be done by string compare as "if Field include 'value'", not as it now "if Field equal to 'value'".
Will your field checkboxes field only have 1 item selected at any given time ? Meaning can the user only select English or German ?
Or can they select both, because in my testing I find that the checkboxes will work in the conditional with only 1 value selected but breaks with multiple values. I've already escalated this issue for re-evaluation.
However we do not have any documentation on how to create the custom conditional shortcode.
If you have some php knowledge then you can use the tool in the link below to auto generate the shortcode but you will need to write the custom comparison logic for the shortcode to work. hidden link
Yes, exactly - if only one is selected condition works perfectly, if both - logic is broken. So, I was sure that this is a bug.
Regarding conditional shortcode - I understand how to write shortcode, but not understand how to use shortcode in condition or what do you mean as Conditional Shortcode
This took some time but I managed to draft up a custom shortcode to help with this. add the following to the custom shortcode settings in Toolset->Settings->Custom Code.
// Put the code of your snippet below this comment.
// Create Shortcode checkboxes_conditional
// Shortcode: [checkboxes_conditional checkbox="" separator=","]Content[/checkboxes_conditional]
function create_checkboxescondition_shortcode($atts, $content = null) {
$atts = shortcode_atts(
array(
'checkbox' => '',
'separator' => ',',
'value' => '',
),
$atts,
'checkboxes_conditional'
);
$checkbox = $atts['checkbox'];
$separator = $atts['separator'];
$value = $atts['value'];
$checkbox_arr = array();
$value_arr = array();
//if both checkbox and value to check are comma separated strings
if (strpos($checkbox,$separator)!== false && strpos($value,$separator)!== false ){
$checkbox_arr = explode($separator,$checkbox);
$value_arr = explode($separator,$value);
$results = array_intersect($value_arr,$checkbox_arr);
if(!empty($results)){
return $content;
}
}
//if checkboxes is comma separated string but value isn't
if(strpos($checkbox,$separator) !== false){
$checkbox_arr = explode($separator,$checkbox);
$value_arr = explode($separator,$value_arr);
if(in_array($value,$checkbox_arr)){
return $content;
}
}
//if value is an array but checkbox isn't
if(strpos($value,$separator) !== false){
$checkbox_arr = explode($separator,$checkbox);
$value_arr = explode($separator,$value);
if(in_array($checkbox_arr,$value_arr)){
return $content;
}
}
// when both are not arrays
if($checkbox == $value){
return $content;
}
}
add_shortcode( 'checkboxes_conditional', 'create_checkboxescondition_shortcode' );
Then activate it.
An example usage is
[checkboxes_conditional checkbox="[types field='checkboxes-group' separator=', '][/types]" separator=',' value='1']This is my content[/checkboxes_conditional]