【问题标题】:Be able to select multiple values in "select" or "checkbox" wordpress能够在“选择”或“复选框”wordpress 中选择多个值
【发布时间】:2018-12-11 18:51:15
【问题描述】:

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

我使用 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'] ) );

            }

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    试试这个代码

           // 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']) );
    
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-04
      • 1970-01-01
      • 2014-10-07
      相关资源
      最近更新 更多