wordpress - WP Metaboxes 多选更新,获取当前
问题描述
目前在我的插件中不起作用的最后一件事是多选输入字段。如图所示,可以选择项目,但更新帖子 1) 不保存选定的值,并且 2) 进入编辑屏幕时不加载它们。
我的代码:
<?php
// METABOX I Diet Information
function diet_add_meta_box1() {
add_meta_box( 'diet_meta_box1', __( 'Diet Information', 'nutriplus' ), 'diet_build_meta_box1', "np-diet", "side", "high", null);
}
add_action( 'add_meta_boxes', 'diet_add_meta_box1' );
/**
* Build custom field METABOX I
*
* @param post $post The post object
*/
function diet_build_meta_box1( $post ){
// make sure the form request comes from WordPress
wp_nonce_field( basename( __FILE__ ), 'diet_meta_box1_nonce' );
// retrieve the current values
$current_diet_goals = get_post_meta( $post->ID, '_diet_diet_goals', false );
?>
<div>
<label for="diet-goals" class="np-label"><strong><?php _e( 'Diet Goals', 'nutriplus' ) ?></strong></label>
<p>
<select name="diet_goals[]" id="np-dropdown" class="np-dropdown" multiple>
<option value="build-body" <?php selected( in_array( 'build-body', $current_diet_goals, true ) ); ?>><?php _e( 'Build Body', 'nutriplus' ) ?></option>
<option value="build-muscle" <?php selected( in_array( 'build-muscle', $current_diet_goals, true ) ); ?>><?php _e( 'Build Muscle', 'nutriplus' ) ?></option>
<option value="eat-healthy" <?php selected( in_array( 'eat-healthy', $current_diet_goals, true ) ); ?>><?php _e( 'Eat Healthy', 'nutriplus' ) ?></option>
<option value="gain-weight" <?php selected( in_array( 'gain-weight', $current_diet_goals, true ) ); ?>><?php _e( 'Gain Weight', 'nutriplus' ) ?></option>
<option value="hold-weight" <?php selected( in_array( 'hold-weight', $current_diet_goals, true ) ); ?>><?php _e( 'Hold Weight', 'nutriplus' ) ?></option>
<option value="improve-condition" <?php selected( in_array( 'improve-condition', $current_diet_goals, true ) ); ?>><?php _e( 'Improve Condition', 'nutriplus' ) ?></option>
<option value="improve-performance" <?php selected( in_array( 'improve-performance', $current_diet_goals, true ) ); ?>><?php _e( 'Improve Performance', 'nutriplus' ) ?></option>
<option value="lose-fat" <?php selected( in_array( 'lose-fat', $current_diet_goals, true ) ); ?>><?php _e( 'Lose Fat', 'nutriplus' ) ?></option>
<option value="lose-weight" <?php selected( in_array( 'lose-weight', $current_diet_goals, true ) ); ?>><?php _e( 'Lose Weight', 'nutriplus' ) ?></option>
</select>
</p>
</div>
<?php
}
function diet_save_meta_box1_data( $post_id ){
// verify meta box nonce
if ( !isset( $_POST['diet_meta_box1_nonce'] ) || !wp_verify_nonce( $_POST['diet_meta_box1_nonce'], basename( __FILE__ ) ) ){
return;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ){
return;
}
// store custom fields values METABOX1
if ( isset( $_REQUEST['diet_goals'] ) ) {
$array = array_map( 'sanitize_text_field', wp_unslash( $_POST['diet_goals'] ) );
update_post_meta( $post_id, '_diet_diet_goals', $array );
}
}
add_action( 'save_post', 'diet_save_meta_box1_data' );
?>
我知道什么可能是错的。也许这部分:
$current_diet_goals = get_post_meta( $post->ID, '_diet_diet_goals', false );
显然 save_meta 函数和我认为的字段名称也是如此。我在互联网上搜索了很多来解决这个问题,但我没有成功镜像我在代码中找到的解决方案:(
谢谢
解决方案
您需要在选择中更改属性名称。
diet_goals[]
改为使用diet_goals
并更改您选择的功能:
selected( in_array( 'some-value', $current_diet_goals, true ) );
推荐阅读
- excel - 使用 getopenfilename 和 vlookup 从关闭的工作簿中获取数据
- jquery - jQuery 函数相互覆盖
- android - Android Studio:引导类路径未与 -source 1.6 一起设置
- python - 何时在条带中的计费周期结束时按月收费
- go - 为golang prometheus收集器添加标签
- ruby-on-rails - 提交时忽略公共上传
- office-js - Excel Online 无法保留 CustomXML 部件
- python - Django:如何在其他 python 脚本中使用表单值?
- javascript - 使用 ajax+js 在 symfony4 中进行实时搜索
- python-2.7 - 如何从文件中提取数据而不使用 Python 去复制