这是我保存多个选择字段值的当前代码。它只保存一(1)个值,而不是我选择的每个值。
public function booked_save_calendars_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = ($term_id);
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
update_option( "taxonomy_$t_id", $term_meta );
}
}
这是在WordPress中为分类法保存自定义元字段。
答案 0 :(得分:0)
根据我所了解的有限信息,我只能回答这个问题。如果符合您的需要,那就接近它。
<form action="" method="post">
<select name="fruits[]" multiple>
<option value='apple'>Apple</option>
<option value='orange'>Orange</option>
<option value='mango'>Mango</option>
<option value='grapes'>Grapes</option>
</select>
<input type="submit" value="submit"/>
</form>
// The name attribute for multi select field should be of array type
<?php
if(isset($_POST['fruits']))
{
foreach($_POST['fruits'] as $key=>$value)
{
echo $value."<br>";
// do something required
}
// or if you want to insert it in db just convert it into JSON data
json_encode($_POST['fruits']);
// proceed the insertion query
// Note The db field for the json data should have text data type
}
?>