这是我以前使用过的代码——在我看来,主要的区别在于您检查的是元是否存在,而不是它的值是确定是否应该检查它。
// Checkbox Meta
add_action("admin_init", "checkbox_init");
function checkbox_init(){
add_meta_box("checkbox", "Checkbox", "checkbox", "post", "normal", "high");
}
function checkbox(){
global $post;
$custom = get_post_custom($post->ID);
$field_id = $custom["field_id"][0];
?>
<label>Check for yes</label>
<?php $field_id_value = get_post_meta($post->ID, \'field_id\', true);
if($field_id_value == "yes") $field_id_checked = \'checked="checked"\'; ?>
<input type="checkbox" name="field_id" value="yes" <?php echo $field_id_checked; ?> />
<?php
}
// Save Meta Details
add_action(\'save_post\', \'save_details\');
function save_details(){
global $post;
if (defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
return $post->ID;
}
update_post_meta($post->ID, "field_id", $_POST["field_id"]);
}