单击更新后,未保存第二个选择列表值

时间:2014-07-15 作者:Rodrigo

我在元数据库中有一个选择列表,在编辑帖子时单击“更新”后,该列表的值被成功保存。当然,这些值是自定义分类法。But i don\'t know how to add another select list and make it save its values successfully too...

add_action( \'add_meta_boxes\', \'so_custom_meta_box\' );

function so_custom_meta_box($post){
add_meta_box(\'so_meta_box\', \'Reproductor 1\', \'custom_element_grid_class_meta_box\',     $post->post_type, \'normal\' 

, \'high\');
}

add_action(\'save_post\', \'so_save_metabox\');

function so_save_metabox(){ 
global $post;

if(isset($_POST["custom_element_grid_class"])){
     //UPDATE: 
    $meta_element_class = $_POST[\'custom_element_grid_class\'];
    //END OF UPDATE

    update_post_meta($post->ID, \'custom_element_grid_class_meta_box\',   $meta_element_class);
    //print_r($_POST);
}
}

function custom_element_grid_class_meta_box($post){
$meta_element_class = get_post_meta($post->ID, \'custom_element_grid_class_meta_box\', true); ?>   

  <select name="custom_element_grid_class" id="custom_element_grid_class">
  <option value="Netu" <?php selected( $meta_element_class, \'Netu\' ); ?>>Netu</option>
  <option value="Netu-latino" <?php selected( $meta_element_class, \'Netu-latino\' ); ? >>Netu Latino</option>
  <option value="Netu-castellano" <?php selected( $meta_element_class, \'Netu- castellano\' ); ?>>Netu Castellano</option>
  <option value="Netu-vose" <?php selected( $meta_element_class, \'Netu-vose\' ); ?>>Netu    Vose</option>
</select>

  <select name="custom_element_grid_class" id="custom_element_grid_class">
  <option value="Vk" <?php selected( $meta_element_class, \'Vk\' ); ?>>Vk</option>
  <option value="Vk-latino" <?php selected( $meta_element_class, \'Vk-latino\' ); ?>>Vk  Latino</option>
  <option value="Vk-castellano" <?php selected( $meta_element_class, \'Vk-castellano\' );  ?>>Vk Castellano</option>
  <option value="Vk-vose" <?php selected( $meta_element_class, \'Vk-vose\' ); ?>>Vk    Vose</option>
</select>

<?php
}
单击“更新”时,仅保存第一个选择列表值,而不保存第二个。我已经知道这必须是保存代码but i can\'t figure out how to be able to modify it so i can put a second select list and its value can be saved.

2 个回复
SO网友:Oleg Butuzov

他们有一个共同的名字。。。尝试将名称声明为数组。

<select name="custom_element_grid_class[]" id="custom_element_grid_class">

SO网友:madalinivascu

尝试以下操作:

     add_action( \'add_meta_boxes\', \'so_custom_meta_box\' );

        function so_custom_meta_box($post){
        add_meta_box(\'so_meta_box\', \'Reproductor 1\', \'custom_element_grid_class_meta_box\',     $post->post_type, \'normal\' 

        , \'high\');
        }

        add_action(\'save_post\', \'so_save_metabox\');

        function so_save_metabox(){ 
        global $post;

        if(isset($_POST["custom_element_grid_class"]) && isset($_POST["custom_element_grid_class2"])){
             //UPDATE: 
            $meta_element_class = $_POST[\'custom_element_grid_class\'];
            $meta_element_class2 = $_POST[\'custom_element_grid_class2\'];
            //END OF UPDATE

            update_post_meta($post->ID, \'custom_element_grid_class_meta_box\',   $meta_element_class);
        update_post_meta($post->ID, \'custom_element_grid_class_meta_box2\',   $meta_element_class2);
            //print_r($_POST);
        }
        }

        function custom_element_grid_class_meta_box($post){
        $meta_element_class = get_post_meta($post->ID, \'custom_element_grid_class_meta_box\', true); 
$meta_element_class2 = get_post_meta($post->ID, \'custom_element_grid_class_meta_box2\', true); ?>  

          <select name="custom_element_grid_class" id="custom_element_grid_class">
          <option value="Netu" <?php selected( $meta_element_class, \'Netu\' ); ?>>Netu</option>
          <option value="Netu-latino" <?php selected( $meta_element_class, \'Netu-latino\' ); ? >>Netu Latino</option>
          <option value="Netu-castellano" <?php selected( $meta_element_class, \'Netu- castellano\' ); ?>>Netu Castellano</option>
          <option value="Netu-vose" <?php selected( $meta_element_class, \'Netu-vose\' ); ?>>Netu    Vose</option>
        </select>

          <select name="custom_element_grid_class2" id="custom_element_grid_class">
          <option value="Vk" <?php selected( $meta_element_class2, \'Vk\' ); ?>>Vk</option>
          <option value="Vk-latino" <?php selected( $meta_element_class2, \'Vk-latino\' ); ?>>Vk  Latino</option>
          <option value="Vk-castellano" <?php selected( $meta_element_class2, \'Vk-castellano\' );  ?>>Vk Castellano</option>
          <option value="Vk-vose" <?php selected( $meta_element_class2, \'Vk-vose\' ); ?>>Vk    Vose</option>
        </select>

        <?php
        }

结束