Wordpress:可重复字段-存储多项选择


Wordpress: Repeatable Fields - Store multiple selections

编辑:

我不知道如何在数组中存储多个选择,只保存第一个值。

img链接->http://oi62.tinypic.com/20p58cl.jpg

有人能帮我吗?

@谢谢你的帮助,但没有用。

请看一下这个代码。顺致敬意,

<?php
/*
Plugin Name: EXAMPLE repeatable-fields
*/

add_action('admin_init', 'add_meta_boxes', 1);
function add_meta_boxes() {
    add_meta_box( 'repeatable-fields', 'EXAMPLE', 'repeatable_meta_box_display', 'post', 'normal', 'high');
}
function repeatable_meta_box_display() {
    global $post;
    $repeatable_fields = get_post_meta($post->ID, 'repeatable_fields', true);
         $item_array = array('A','B','C','D');

    wp_nonce_field( 'repeatable_meta_box_nonce', 'repeatable_meta_box_nonce' );
?>
    <script type="text/javascript">
jQuery(document).ready(function($) {
    $('.metabox_submit').click(function(e) {
        e.preventDefault();
        $('#publish').click();
    });
    $('#add-row').on('click', function() {
        var row = $('.empty-row.screen-reader-text').clone(true);
        row.removeClass('empty-row screen-reader-text');
        row.insertBefore('#repeatable-fieldset-one tbody>tr:last');
        return false;
    });
    $('.remove-row').on('click', function() {
        $(this).parents('tr').remove();
        return false;
    });
    $('#repeatable-fieldset-one tbody').sortable({
        opacity: 0.6,
        revert: true,
        cursor: 'move',
        handle: '.sort'
    });
});
    </script>
    <table id="repeatable-fieldset-one" width="100%">
    <thead>
        <tr>
            <th width="2%"></th>
            <th width="60%">Name</th>
            <th width="60%">Surname</th>
            <th width="10%">Item</th>
            <th width="2%"></th>
        </tr>
    </thead>
    <tbody>
    <?php
    if ( $repeatable_fields ) :
        foreach ( $repeatable_fields as $field ) {
?>
    <tr>
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" value="<?php if($field['name'] != '') echo esc_attr( $field['name'] ); ?>" /></td>
        <td><input type="text" class="widefat" name="surname[]" value="<?php if ($field['surname'] != '') echo esc_attr( $field['surname'] ); else echo ''; ?>" />
        </td>

        <td>  
     <?php
        echo '
        <select name="item[]" multiple>';
        foreach ( $item_array as $label => $value ) : 
        echo '<option value="'.$value.'" '.selected( $field['item'], $value,true ).'>'.$value.'</option>';
        endforeach; 
        echo '</select>';
     ?>
        </td>

        <td><a class="sort">|||</a></td>
    </tr>
    <?php
        }
    else :
        // show a blank one
?>
    <tr>
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" /></td>

        <td><input type="text" class="widefat" name="surname[]" value="" /></td>
        <td>  
     <?php
        echo '
        <select name="item[]" multiple>';
        foreach ( $item_array as $label => $value ) : 
        echo '<option value="'.$value.'" '.selected( $field['item'], $value,true ).'>'.$value.'</option>';
        endforeach; 
        echo '</select>';
     ?>
        </td>
<td><a class="sort">|||</a></td>
    </tr>
    <?php endif; ?>
    <!-- empty hidden one for jQuery -->
    <tr class="empty-row screen-reader-text">
        <td><a class="button remove-row" href="#">-</a></td>
        <td><input type="text" class="widefat" name="name[]" /></td>

        <td><input type="text" class="widefat" name="surname[]" value="" /> </td>
        <td>  
     <?php
        echo '
        <select name="item[]" multiple>';
        foreach ( $item_array as $label => $value ) : 
        echo '<option value="'.$value.'" '.selected( $field['item'], $value,true ).'>'.$value.'</option>';
        endforeach; 
        echo '</select>';
     ?>
        </td>
<td><a class="sort">|||</a></td>
    </tr>
    </tbody>
    </table>
    <p><a id="add-row" class="button" href="#">Add another</a>
    <input type="submit" class="metabox_submit" value="Save" />
    </p>
    <?php
}
add_action('save_post', 'repeatable_meta_box_save');
function repeatable_meta_box_save($post_id) {
    if ( ! isset( $_POST['repeatable_meta_box_nonce'] ) ||
        ! wp_verify_nonce( $_POST['repeatable_meta_box_nonce'], 'repeatable_meta_box_nonce' ) )
        return;
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;
    if (!current_user_can('edit_post', $post_id))
        return;
    $old = get_post_meta($post_id, 'repeatable_fields', true);
    $new = array();

    $names = $_POST['name'];
    $surnames = $_POST['surname'];
    $items = $_POST['item'];
    $count = count( $names );
    for ( $i = 0; $i < $count; $i++ ) {
        if ( $names[$i] != '' ) :
            $new[$i]['name'] = stripslashes( strip_tags( $names[$i] ) );
            $new[$i]['surname'] = stripslashes( $surnames[$i] );
            $new[$i]['item'] = stripslashes( strip_tags( $items[$i] ) );

        endif;
    }
    if ( !empty( $new ) && $new != $old )
        update_post_meta( $post_id, 'repeatable_fields', $new );
    elseif ( empty($new) && $old )
        delete_post_meta( $post_id, 'repeatable_fields', $old ); }
?>

我认为应该为输入数据添加一个索引。我制作了一个示例php页面,其中包含两个包含相同输入类型的块

<html>
<head></head>
<body>
<h1>POST VALUES</h1>
<pre><?php print_r($_POST); ?></pre>
<h1>SAMPLE FORM</h1>
<form method="POST">
<h2>first block</h2>
first name 1<input name="first_name[1]" />
<br />last name 1<input name="last_name[1]" />
<br /><select name="item[1][]" multiple>
<option value="1">primo</option>
<option value="2">secondo</option>
</select>
<h2>second block</h2>
first name 2<input name="first_name[2]" />
<br />last name 2<input name="last_name[2]" />
<br /><select name="item[2][]" multiple>
<option value="1">primo</option>
<option value="2">secondo</option>
</select>
<br /><input type="submit" value="submit">
</form>
</body>
</html>

以这种方式,当您提交您在$_POST 中的数据时

Array
(
    [first_name] => Array
        (
            [1] => name 1
            [2] => name 2
        )
    [last_name] => Array
        (
            [1] => last name 1
            [2] => last name 2
        )
    [item] => Array
        (
            [1] => Array
                (
                    [0] => 1
                    [1] => 2
                )
            [2] => Array
                (
                    [0] => 2
                    [1] => 3
                )
        )
)

当你打印你的方框应该很简单添加一个索引到输入数据。类似的东西

$index = 0;
foreach ($informations as $field): 
  $index++;
  echo '<input name="name['.$index.'][]" value="'. esc_attr($field['name']) .'" />';
  echo '<input name="surname['.$index.'][]" value="'. esc_attr($field['surname']) .'" />';
  ....

希望这能有所帮助!