So I'm using a checkbox field, and I check it's value by using the code below and print things out accordingly. Anyway, if the field's checkboxes don't have any value, meaning all of them are unchecked, I get an error.
Warning: in_array() expects parameter 2 to be array, boolean given in /filepath.php on line 647
<?php if (in_array( 'Subbed', get_field('episode_sdversion'))) { ?>
<a href="<?php echo $episode_permalink; ?>#subbed">Subbed</a>
<?php } else {
echo '--';
} ?>
So basically, what can I do with this code to make it so when all values are unchecked, that would automatically mean that "Subbed" value is also unchecked, so it should simply just show echo '--';. So how can I make this echo '--'; run when all values are unchecked. So it shouldn't come up with that error?
I'm not sure what your get_field() function does, presumably it's part of a framework or something, but I'm guessing it returns the value of $_REQUEST['episode_sdversion'], which will be FALSE when the checkbox is empty.
In this case, if I understand your question correctly, a simple check to first see if get_field() is returning something other than FALSE would suffice:
<?php if (get_field('episode_sdversion') && in_array('Subbed', get_field('episode_sdversion'))) { ?>
<a href="<?php echo $episode_permalink; ?>#subbed">Subbed</a>
<?php } else {
echo '--';
} ?>
You are getting the error because get_field returns false instead of an array when no boxes are checked. The && operator is short circuit, meaning that if the first part gets evaluated as false, the second part won't get executed. So you can avoid the error by replacing your first line (if in_array(...)) with
if(get_field('episode_sdversion) && in_array('Subbed', get_field('episode_sdversion')))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With