I’m showing the checkbox selected by the user from the frontend and everything works fine when I have at least one checkbox selected, but the problem happens when there is no checkbox selected, the code I have is:
<?php
$options = get_field('preferences', 'user_'.$current_user->ID);
$field = get_field_object('preferences','user_'.$current_user->ID);
?>
<input type="hidden" value="<?php echo 'acf['.$field['key'].']' ?>"/>
<?php
if( $field ){
foreach ( $field['choices'] as $key => $value) {
if ( is_array($options && in_array( $key, $options ) ) {
$checked = 'checked';
}else{
$checked = '';
}
echo '<p class="field"><input type="checkbox" '.$checked.' name="acf['.$field['key'].'][]" id="preferences" value="'.$key.'">'.$value.'</p>';
}
}
?>
Help me please. Thank you
Aucun commentaire:
Enregistrer un commentaire