0
我不明白如何將多個選擇存儲陣列中, 只保存第一個值。
IMG鏈接 - >http://oi62.tinypic.com/20p58cl.jpg
有人能幫助我嗎?
@maronl謝謝你的幫助,但它沒有奏效。
請看看這段代碼。 最好的問候。
<?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); }
?>
你好maroni,謝謝你的幫助,但它沒有奏效。請參閱完整的代碼。 –
你好。在你的代碼中,我看不到我建議的索引。輸入的名字仍然是名字[],姓氏[],項目[],他們應該是第一個人的名字[1],姓[1],項[1] []。爲第二個名字[2],姓氏[2],項目[2] []。通過這種方式,您可以讓每個人都有不同的商品,並且不會將其合併到獨特的陣列中。那麼當添加新人時,你還必須在你的jQuery代碼中管理該索引 – maronl