经过几天的试验,我终于把这个坚果弄碎了。
<!-- Note how name attribute has actual number in brackets -->
<input type="checkbox" name="feature[1]" id="feature-one" value="feature_one"<?php if ( $feature_one ) { echo \'checked\'; } ?> />
<input type="checkbox" name="feature[2]" id="feature-one" value="feature_one" <?php if ( $feature_one ) { echo \'checked\'; } ?> />
<?php
//This happens on form submit or when you press edit button
//Number or checkboxes goes here which eventually become keys
$checkboxes = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);
$features = array();
$p = array_key_exists(\'feature\', $_POST) ? $_POST[\'feature\'] : array();
foreach( $checkboxes as $i ) {
if(array_key_exists( $i, $p ) ) {
//If is checked
$features[$i] = 1;
}
else {
//If is not checked
$features[$i] = 0;
}
}
//Save to database as an array where checked are 1 and unchecked are 0
update_term_meta( $term_id, \'features\', $features );
?>