【发布时间】:2020-02-07 22:26:24
【问题描述】:
如何选择所有选中的选项并填写我的自定义帖子类型复选框字段?
这是表格:
<form action="#" method="post">
<div class="row">
<div class="column left labels">
<input type="checkbox" name="grips[]" value="Top (T1)"><label>Top (T1)</label>
<input type="checkbox" name="grips[]" value="High (12)"><label>High (12)</label>
</div>
<div class="column left labels">
<input type="checkbox" name="grips[]" value="Mid (23)"><label>Mid (23)</label>
<input type="checkbox" name="grips[]" value="Low (34)"><label>Low (34)</label>
</div>
<div class="column left labels">
<input type="checkbox" name="grips[]" value="Bottom (P4)"><label>Bottom (P4)</label>
<input type="checkbox" name="grips[]" value="Other"><label>Other</label>
</div>
</div>
<input type="submit" name="submit" value="Submit"/>
</form>
我想这样做,但这不起作用,它只会取数组中的最后一个值:
__update_post_meta( $the_post_id, 'grips', $_POST['grips']);
我试图循环遍历,但这将删除所有其他复选标记并只留下最后一个添加的复选标记:
foreach($_POST['grips'] as $selected){
echo $selected."</br>";
__update_post_meta( $the_post_id, 'grips', $selected);
}
这就是我的 Functions.php 文件中的内容: 我想我需要另一个数组函数,因为这个函数只更新单个自定义字段:
function __update_post_meta( $post_id, $field_name, $value = '' ) {
if ( empty( $value ) OR ! $value )
{
delete_post_meta( $post_id, $field_name );
}
elseif ( ! get_post_meta( $post_id, $field_name ) )
{
add_post_meta( $post_id, $field_name, $value );
}
else
{
update_post_meta( $post_id, $field_name, $value );
}
}
【问题讨论】:
-
您是否尝试过打印 $_POST 数据?例如
var_dump($_POST); die;。如果帖子数据实际上具有所有值,这将帮助您调试。 -
@LeviCole 是的,我有,数据在那里:["grips"]=> array(3) { [0]=> string(8) "Top (T1)" [1] => string(8) "低 (34)" [2]=> string(11) "底部 (P4)" }
标签: php html wordpress metadata custom-wordpress-pages