为什么不只使用一个metabox,然后使用jQuery管理要显示的选项:
// add metabox
function adding_custom_meta_boxes( $post_type, $post ) {
add_meta_box(
\'my-meta-box\',
__( \'My Meta Box\' ),
\'render_my_meta_box\',
\'post\',
\'normal\',
\'default\'
);
}
add_action( \'add_meta_boxes\', \'adding_custom_meta_boxes\', 10, 2 );
// render metabox
function render_my_meta_box( $post ) {
wp_nonce_field( \'my_metas_nonce\', \'my_metas_nonce\' );
$my_features_1 = get_post_meta( $post->ID, \'my_features_1\', true );
$my_option_1 = get_post_meta( $post->ID, \'my_option_1\', true );
$my_features_2 = get_post_meta( $post->ID, \'my_features_2\', true );
$my_option_2 = get_post_meta( $post->ID, \'my_option_2\', true );
$my_features_3 = get_post_meta( $post->ID, \'my_features_3\', true );
$my_option_3 = get_post_meta( $post->ID, \'my_option_3\', true );
?>
<input type="checkbox" name="my_features_1" class="my_features" value="my_option_1" <?php checked($my_features_1, \'my_option_1\'); ?>>Option 1<br>
<input type="checkbox" name="my_features_2" class="my_features" value="my_option_2" <?php checked($my_features_2, \'my_option_2\'); ?>>Option 2<br>
<input type="checkbox" name="my_features_3" class="my_features" value="my_option_3" <?php checked($my_features_3, \'my_option_3\'); ?>>Option 3<br>
<div id="my_option_1" class="my_options" style="<?php if( ! $my_features_1 ) echo \'display: none;\'; ?>">
<h3>Option 1</h3>
<input name="my_option_1" value="<?php echo $my_option_1; ?>" />
</div>
<div id="my_option_2" class="my_options" style="<?php if( ! $my_features_2 ) echo \'display: none;\'; ?>">
<h3>Option 2</h3>
<input name="my_option_2" value="<?php echo $my_option_2; ?>" />
</div>
<div id="my_option_3" class="my_options" style="<?php if( ! $my_features_3 ) echo \'display: none;\'; ?>">
<h3>Option 3</h3>
<input name="my_option_3" value="<?php echo $my_option_3; ?>" />
</div>
<script>
jQuery(document).ready(function($) {
$(\'.my_features\').change(function() {
var checkbox = $(this);
if( checkbox.is(\':checked\') ) {
$( \'#\' + checkbox.val() ).show();
} else {
$( \'#\' + checkbox.val() ).hide();
}
});
});
</script>
<?php
}
// save metabox data
function save_post_features_meta( $post_id ){
if ( ! isset( $_POST[\'my_metas_nonce\'] ) || ! wp_verify_nonce( $_POST[\'my_metas_nonce\'], \'my_metas_nonce\' ) || ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) || ! current_user_can( \'edit_post\', $post_id ) ) {
return;
}
update_post_meta( $post_id, \'my_features_1\', sanitize_text_field($_POST[\'my_features_1\']));
update_post_meta( $post_id, \'my_option_1\', sanitize_text_field($_POST[\'my_option_1\']));
update_post_meta( $post_id, \'my_features_2\', sanitize_text_field($_POST[\'my_features_2\']));
update_post_meta( $post_id, \'my_option_2\', sanitize_text_field($_POST[\'my_option_2\']));
update_post_meta( $post_id, \'my_features_3\', sanitize_text_field($_POST[\'my_features_3\']));
update_post_meta( $post_id, \'my_option_3\', sanitize_text_field($_POST[\'my_option_3\']));
}
add_action( \'save_post\', \'save_post_features_meta\');