我知道有很多关于这方面的问题,但我还没有找到答案。
我在帖子类型上有一个metaboxpage, 只包含一个复选框。看来不管我做什么都救不了我。以下是metabox的所有代码。
/*--------------------------------------------------------------------------*
 * Register metabox
/*--------------------------------------------------------------------------*/
function kasparabi_page_left_menu() {
    add_meta_box( \'kasparabi-left-menu-meta\', __( \'Left Menu\', \'kasparabi\' ), \'kasparabi_render_left_menu_meta_box\', \'page\', \'side\' );
}
add_action( \'add_meta_boxes\', \'kasparabi_page_left_menu\' );
/*--------------------------------------------------------------------------*
 * Callbacks
/*--------------------------------------------------------------------------*/
function kasparabi_render_left_menu_meta_box($post) {
    wp_nonce_field( basename( __FILE__ ), \'kasparabi-left-menu-meta_nonce\' );
    ?>
        <p>
            <div>
                <label for="left-menu-checkbox">
                    <input type="checkbox" name="left-menu-checkbox" <?php (get_post_meta( $post->ID, \'left_menu_checkbox\', true) == \'on\') ? \' checked="checked"\' : \'\'; ?> />
                    <?php _e( \'Display left menu\', \'kasparabi\' )?>
                </label>
            </div>
        </p>
    <?php
}
/*--------------------------------------------------------------------------*
 * Save functions
/*--------------------------------------------------------------------------*/
function kasparibi_left_menu_meta_save( $post_id, $post ) {
    // Checks save status
    $is_autosave = wp_is_post_autosave( $post_id );
    $is_revision = wp_is_post_revision( $post_id );
    $is_valid_nonce = ( isset( $_POST[ \'kasparabi-left-menu-meta_nonce\' ] ) && wp_verify_nonce( $_POST[ \'kasparabi-left-menu-meta_nonce\' ], basename( __FILE__ ) ) ) ? \'true\' : \'false\';
    // Exits script depending on save status
    if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
        return $post_id;
    }
    /* Get the post type object. */
    $post_type = get_post_type_object( $post->post_type );
    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;
    /* Get the posted data and sanitize it for use as an HTML class. */
    $new_meta_value = ( isset( $_POST[\'left-menu-checkbox\'] ) ? sanitize_html_class( $_POST[\'left-menu-checkbox\'] ) : \'\' );
    $meta_key = \'left_menu_checkbox\';
    $meta_value = get_post_meta( $post->ID, $meta_key, true);
    /* If a new meta value was added and there was no previous value, add it. */
    if ( $new_meta_value && \'\' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true);
    /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );
    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( \'\' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );
}
add_action( \'save_post\', \'kasparibi_left_menu_meta_save\' );
 
                    最合适的回答,由SO网友:s_ha_dum 整理而成
                    我注意到了几件事。
首先,回调使用两个参数,但在挂接它时不要求第二个参数。这是:
add_action( \'save_post\', \'kasparibi_left_menu_meta_save\' );
 应为:
add_action( \'save_post\', \'kasparibi_left_menu_meta_save\' ,1 ,2 );
 但是,另一方面,我看不出在哪里使用这个参数。也许我错过了。您可以将第二个参数保留在回调之外。
但真正的问题在于:
<?php (get_post_meta( $post->ID, \'left_menu_checkbox\', true) == \'on\') ? \' checked="checked"\' : \'\'; ?>
 你没有
echo 这个
checked 属性
<?php echo (get_post_meta( $post->ID, \'left_menu_checkbox\', true) == \'on\') ? \' checked="checked"\' : \'\'; ?>
 换言之,该值确实保存,但您的代码无法正确显示选中状态。
你可以用WordPress的checked 但功能。
<?php checked(get_post_meta( $post->ID, \'left_menu_checkbox\', true),\'on\',true) ?>