我也为此绞尽脑汁,终于找到了一个可行的解决方案。这应该对你有用,我已经很快测试过了。我将分三部分介绍:
PART 1: ADD THE CUSTOM META BOX
add_action( \'add_meta_boxes\', \'add_custom_box\' );
function add_custom_box( $post ) {
add_meta_box(
\'Meta Box\', // ID, should be a string.
\'Featured People\', // Meta Box Title.
\'people_meta_box\', // Your call back function, this is where your form field will go.
\'post\', // The post type you want this to show up on, can be post, page, or custom post type.
\'side\', // The placement of your meta box, can be normal or side.
\'core\' // The priority in which this will be displayed.
);
}
PART 2: DEFINE THE "CALLBACK" FUNCTION (spits out the html for the checkboxes)
function people_meta_box($post) {
wp_nonce_field( \'my_awesome_nonce\', \'awesome_nonce\' );
$checkboxMeta = get_post_meta( $post->ID );
?>
<input type="checkbox" name="bob" id="bob" value="yes" <?php if ( isset ( $checkboxMeta[\'bob\'] ) ) checked( $checkboxMeta[\'bob\'][0], \'yes\' ); ?> />Bob<br />
<input type="checkbox" name="bill" id="bill" value="yes" <?php if ( isset ( $checkboxMeta[\'bill\'] ) ) checked( $checkboxMeta[\'bill\'][0], \'yes\' ); ?> />Bill<br />
<input type="checkbox" name="steve" id="steve" value="yes" <?php if ( isset ( $checkboxMeta[\'steve\'] ) ) checked( $checkboxMeta[\'steve\'][0], \'yes\' ); ?> />Steve<br />
<?php }
PART 3: SAVE THE CHECKBOX VALUES
add_action( \'save_post\', \'save_people_checkboxes\' );
function save_people_checkboxes( $post_id ) {
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
if ( ( isset ( $_POST[\'my_awesome_nonce\'] ) ) && ( ! wp_verify_nonce( $_POST[\'my_awesome_nonce\'], plugin_basename( __FILE__ ) ) ) )
return;
if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] ) ) {
if ( ! current_user_can( \'edit_page\', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
}
//saves bob\'s value
if( isset( $_POST[ \'bob\' ] ) ) {
update_post_meta( $post_id, \'bob\', \'yes\' );
} else {
update_post_meta( $post_id, \'bob\', \'no\' );
}
//saves bill\'s value
if( isset( $_POST[ \'bill\' ] ) ) {
update_post_meta( $post_id, \'bill\', \'yes\' );
} else {
update_post_meta( $post_id, \'bill\', \'no\' );
}
//saves steve\'s value
if( isset( $_POST[ \'steve\' ] ) ) {
update_post_meta( $post_id, \'steve\', \'yes\' );
} else {
update_post_meta( $post_id, \'steve\', \'no\' );
}
}