能够在“选择”或“复选框” wordpress中选择多个值

时间:2018-07-03 12:38:34

标签: php html wordpress

如何在列表框中选择多个值。我尝试使用下面的代码,它只选择一次值。请提出建议,并提供另一种方法来同时选择多个值:

我使用wordpress,这是我的代码:

            // Add metabox
            add_action( 'add_meta_boxes', 'cpt_news_filiale_meta_box_add' );
            function cpt_news_filiale_meta_box_add()
            {
                add_meta_box( 'gps-meta-box-id', 'Filiale', 'cpt_entreprise_news_filiale_meta_box_display', 'newsdesfiliales', 'normal', 'high' );
            }

            // Dislay metabox
            function cpt_entreprise_news_filiale_meta_box_display( $post )
            {
                $args = array(
                    'numberposts' => 999,
                    'post_type' => 'entreprise_sector',
                    'status' => 'publish',
                    'suppress_filters' => 0,
                );
                $entreprises = get_posts( $args );

                $values = get_post_custom( $post->ID );
                $text = isset( $values['filiale_referente[]'] ) ? esc_attr( $values['filiale_referente'][0] ) : '';
                ?>
                <p>
                    <label for="filiale_referente">Filiale référente : </label>
                    <select name='filiale_referente' id='filiale_referente' multiple>
                        <?php foreach ($entreprises as $entreprise): ?>
                        <option <?php if($text == $entreprise->ID ) :?> selected="true" <?php endif;?>value="<?php echo esc_attr($entreprise->ID); ?>"><?php echo esc_html($entreprise->post_title); ?></option>
                        <?php endforeach; ?>
                    </select>
                </p>

                <?php   
            }

            // Save metabox
            add_action( 'save_post', 'cpt_news_filiale_meta_box_save' );
            function cpt_news_filiale_meta_box_save( $post_id )
            {
                if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
                if( !current_user_can( 'edit_post', $post_id ) ) return;

                if( isset( $_POST['filiale_referente'] ) )
                    update_post_meta( $post_id, 'filiale_referente', wp_kses( $_POST['filiale_referente'] ) );

            }

1 个答案:

答案 0 :(得分:0)

尝试此代码

       // Add metabox
        add_action( 'add_meta_boxes', 'cpt_news_filiale_meta_box_add' );
        function cpt_news_filiale_meta_box_add()
        {
           add_meta_box( 'gps-meta-box-id', 'Filiale', 'cpt_entreprise_news_filiale_meta_box_display', 'newsdesfiliales', 'normal', 'high' );
        }

        // Dislay metabox
        function cpt_entreprise_news_filiale_meta_box_display( $post )
        {
            $args = array(
                'numberposts' => 999,
                'post_type' => 'entreprise_sector',
                'status' => 'publish',
                'suppress_filters' => 0,
            );
            $entreprises = get_posts( $args );

            $values = get_post_meta( $post->ID,'filiale_referente', true);

            $valuesArr = explode(",",$values);
            ?>
            <p>
                <label for="filiale_referente">Filiale référente : </label>
                <select name='filiale_referente[]' id='filiale_referente' multiple>
                    <?php foreach ($entreprises as $entreprise): ?>
                    <option <?php if( in_array($entreprise->ID,$valuesArr)) :?> selected="true" <?php endif;?>value="<?php echo esc_attr($entreprise->ID); ?>"><?php echo esc_html($entreprise->post_title); ?></option>
                    <?php endforeach; ?>
                </select>
            </p>

            <?php   
        }

        // Save metabox
        add_action( 'save_post', 'cpt_news_filiale_meta_box_save' );
        function cpt_news_filiale_meta_box_save( $post_id )
        {
            if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
            if( !current_user_can( 'edit_post', $post_id ) ) return;


            if( isset( $_POST['filiale_referente'] ) )
                update_post_meta( $post_id, 'filiale_referente', implode(",",$_POST['filiale_referente']) );

        }