无法保存metabox复选框

时间:2017-09-06 04:46:39

标签: wordpress checkbox meta-boxes

我创建了一个插件,我在metabox中列出了带复选框的已发布页面。问题是这些复选框值没有保存到DB。

function display_pages_metabox($post){ 
    $custom = get_post_custom($post->ID);
    $checkfield = maybe_unserialize( get_post_meta($post->ID, "checkfield", true) );

   wp_nonce_field( 'save_quote_meta', 'custom_nonce' );
   $pages = get_pages();

  foreach ( $pages as $page ) { ?>   
      <input id="page_<?php echo $page->ID; ?>" type="checkbox" name="checkfield[]" value="<?php echo $page->ID; ?>" <?php if ( in_array($page->ID, (array) $checkfield) ) { ?> checked <?php } ?>/>

      <label for="page_<?php echo $page->ID; ?>"><?php echo $page->post_title; ?></label> <br>   
    <?php }
     }
function checkfield(){
    global $post;
    if(isset( $_POST['checkfield[]'] )){   

    $custom = $_POST['checkfield[]'];
    $old_meta = get_post_meta($post->ID, '_checkfield[]', true);                    
        if(!empty($old_meta)){
            update_post_meta($post->ID, '_checkfield[]', $custom);
        } else {
        add_post_meta($post->ID, '_checkfield[]', $custom, true);
        }
 }
}

0 个答案:

没有答案