在wordpress中更新post meta上保存多个复选框

时间:2015-11-30 12:31:08

标签: wordpress checkbox custom-post-type

我解决了同样的问题:

http://stackoverflow.com/questions/22660822/how-to-save-multiple-checkbox-on-update-post-meta-in-wordpress

我有一个带有多个复选框元框的cpt(clinica)。

我创建了元数据集:

add_meta_box('custom-meta-box', 'Music', 'music_meta_box', 'clinica', 'advanced', 'core');

使用了建议的代码,但我收到错误:

  

警告:in_array()期望参数2为数组,布尔值为   C:\ WAMP \ WWW \ DWA \可湿性粉剂内容\插件\网络cliniche \网络cliniche.php   第86行

我做错了什么?

我添加$ callback_args但我仍然收到相同的错误消息。

这是我的代码:

    add_action( 'init', 'network_cliniche_register_post_type' );

    function network_cliniche_register_post_type() {
        $cliniche_args = array(
            'public' => true,
            'query_var' => 'clinica',
            'rewrite' => array(
            'slug' => 'clinica',
            'with_front' => false
            ),

            'description' => 'Elenco cliniche appartenti al network',
            'has_archive' => 'true',
            'capability_type' => 'post',
            'taxonomies'          => array( 'clinica_tax' ),
            'menu_icon' => 'dashicons-networking',
            'supports' => array('title', 'editor', 'thumbnail'),
            'labels' => array(
                'name' => 'Cliniche',
                'singular_name' => 'Clinica',
                'all_items' => 'Tutte le cliniche',
                'add_new' => 'Aggiungi clinica ',
                'add_new_item' => 'Aggiungi clinica',
                'edit_item' => 'Modifica clinica',
                'new_item' => 'Nuova clinica',
                'view_item' => 'Visualizza clinica',
                'search_items' => 'Cerca cliniche',
                'not_found' => 'Nessuna clinica registrata',
                'not_found_in_trash' => 'Non sono presenti cliniche nel cestino'
        ),
        'register_meta_box_cb' => 'add_clinica_metaboxes'
        );

        register_post_type( 'clinica', $cliniche_args );

        function add_clinica_metaboxes() {
add_meta_box('dettagli-clinica', 'Dettagli clinica', 'dettagli_clinica', 'clinica', 'advanced', 'core');
            add_meta_box('custom-meta-box', 'Music clinica', 'music_meta_box', 'clinica', 'advanced', 'core', $callback_args);
        }
    }

metabox来自:How to save multiple checkbox on update post meta in wordpress?

function music_meta_box()
{
global $post;
    // Get post meta value using the key from our save function in the second paramater.
$custom_meta = get_post_meta($post->ID, '_custom-meta-box', true);

    ?>
        <input type="checkbox" name="custom-meta-box[]" value="huge" <?php echo (in_array('huge', $custom_meta)) ? 'checked="checked"' : ''; ?> />Huge
        <br>
        <input type="checkbox" name="custom-meta-box[]" value="house" <?php echo (in_array('house', $custom_meta)) ? 'checked="checked"' : ''; ?> />House
        <br>
        <input type="checkbox" name="custom-meta-box[]" value="techno" <?php echo (in_array('techno', $custom_meta)) ? 'checked="checked"' : ''; ?> />Techno<br>
    <?php 
}
add_action( 'save_post', 'save_music_meta_box' );


function save_music_meta_box()
{

    global $post;
    // Get our form field
    if(isset( $_POST['custom-meta-box'] ))
    {
        $custom = $_POST['custom-meta-box'];
        $old_meta = get_post_meta($post->ID, '_custom-meta-box', true);
        // Update post meta
        if(!empty($old_meta)){
            update_post_meta($post->ID, '_custom-meta-box', $custom);
        } else {
            add_post_meta($post->ID, '_custom-meta-box', $custom, true);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

add_meta_box('custom-meta-box', 'Music', 'music_meta_box', 'clinica', 'advanced', 'core');

应该是

add_meta_box('custom-meta-box', 'Music', 'music_meta_box', 'clinica', 'advanced', 'core', $callback_args);

其中$callback_args可以是具有返回类型数组或数组本身的函数。

请参阅codex for add_meta_box