Meta box checkbox won't save

时间:2014-12-20 作者:Bryony Lloyd

我有几个字段,大部分是文本输入,用于将其他信息保存到帖子中,所有文本框都保存了,但我无法获取复选框来保存值!

想知道你们这些天才中是否有人能帮助我。提前谢谢。

下面是复选框,我在复选框输入中回显了ck\\U销售

$ck_sale = get_post_meta($post->ID, \'ck_sale_item\', true); ?>

<?php //Save the information in the metabox
add_action( \'save_post\', \'mb_product_details_save\');
function mb_product_details_save( $post_id ) {
     // Bail if we\'re doing an auto save
    if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) {
        return;  
    }
    // if our nonce isn\'t there, or we can\'t verify it, bail
    if( !isset( $_POST[\'product_details_nonce\'] ) || !wp_verify_nonce( $_POST[\'product_details_nonce\'], \'mb_product_details_nonce\' ) ) {
        return;    
    }
    // if our current user can\'t edit this post, bail
    if( !current_user_can( \'edit_post\' ) ) {
        return;
    }

    //Set the data in the fields before saving
    if( isset( $_POST[\'tb_RRP_price\'] ) ) {
        update_post_meta( $post_id, \'tb_RRP_price\', wp_kses($_POST[\'tb_RRP_price\']));
    }
     if( isset( $_POST[\'tb_trade_price\'] ) ) {
        update_post_meta( $post_id, \'tb_trade_price\', wp_kses($_POST[\'tb_trade_price\']));
    }
     if( isset( $_POST[\'tb_colours\'] ) ) {
        update_post_meta( $post_id, \'tb_colours\', wp_kses($_POST[\'tb_colours\']));
    }
     if( isset( $_POST[\'tb_quantity\'] ) ) {
        update_post_meta( $post_id, \'tb_quantity\', wp_kses($_POST[\'tb_quantity\']));
    }

    if($_POST["ck_sale_item"] == "on") {
        $featured = "on";
    }
    else {
        $featured = "off";
    }
    update_post_meta($post->ID, \'ck_sale_item\', $featured);
} ?>

1 个回复
SO网友:Bryony Lloyd

我现在找到了答案,这是我试图保存它的方式这是我在保存功能中所做的

    if( isset( $_POST[ \'ck_sale_item\' ] ) ) {
    update_post_meta( $post_id, \'ck_sale_item\', \'on\' );
} else {
    update_post_meta( $post_id, \'ck_sale_item\', \'off\' );
}

结束