我在Wordpress的前端有一个表单,它保存了除复选框数组之外的所有字段。如果我在后端帖子编辑器中勾选框,并且勾号正确显示在前面,它将显示并保存正确的值。然而,在前端勾选方框并不能保存!所有其他字段从字体和背面正确保存。我花了很长时间试图弄明白这一点,但它似乎不起作用,任何帮助都是greatly 非常感谢。代码如下:
$query = new WP_Query( array( \'post_type\' => \'property\', \'posts_per_page\' => \'-1\' ) );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
if(isset( $_GET[\'post\'] ) ) {
if ( $_GET[\'post\'] == $post->ID ) {
$current_post = $post->ID;
$options = $custom_meta_fields[7][\'options\'];
$checkboxes = get_post_meta($current_post, \'custom_cgtest\', true);
}
}
endwhile; endif;
wp_reset_query();
global $current_post;
if(isset($_POST[\'submitted\']) && isset($_POST[\'post_nonce_field\']) && wp_verify_nonce($_POST[\'post_nonce_field\'], \'post_nonce\')) {
$post_information = array(
\'ID\' => $current_post,
\'post_title\' => ($_POST[\'post_title\']),
\'post_content\' => esc_attr(strip_tags($_POST[\'postContent\'])),
\'post-type\' => \'property\',
\'post_status\' => \'publish\',
);
$post_id = wp_update_post($post_information);
if($post_id) {
update_post_meta($post_id, \'custom_cgtest\', ($_POST[\'cbn\']));
}
}
在页面中:
foreach ($options as $option) {
echo \'<input type="checkbox" name="cbn[]" id="\'.$option[\'value\'].\'"\',$checkboxes && in_array($option[\'value\'], $checkboxes) ? \' checked="checked"\' : \'\',\' /> \';
echo $option[\'value\'];
}
在函数中。php:
array (
\'label\' => \'Checkbox Group\',
\'desc\' => \'A description for the field.\',
\'id\' => $prefix.\'cgtest\',
\'type\' => \'cgtest\',
\'options\' => array (
\'one\' => array (
\'label\' => \'Option One\',
\'value\' => \'one\'
),
\'two\' => array (
\'label\' => \'Option Two\',
\'value\' => \'two\'
),
\'three\' => array (
\'label\' => \'Option Three\',
\'value\' => \'three\'
)
)
)
如果你需要更多的代码,请告诉我。
SO网友:OriginalEXE
你检查过数据库中的数据了吗?
我猜问题就在这里:
echo \'<input type="checkbox" name="cbn[]" id="\'.$option[\'value\'].\'"\',$checkboxes && in_array($option[\'value\'], $checkboxes) ? \' checked="checked"\' : \'\',\' /> \';
应为:
echo \'<input type="checkbox" name="cbn[]" id="\'.$option[\'value\'].\'"\'.$checkboxes && in_array($option[\'value\'], $checkboxes) ? \' checked="checked"\' : \'\'.\' /> \';
编辑:你把可怜的PHP弄糊涂了。尝试将第三个函数括在括号中。
echo \'<input type="checkbox" name="cbn[]" id="\' . $option[\'value\'] . \'"\' . ( $checkboxes && in_array( $option[\'value\'], (array) $checkboxes ) ? \' checked="checked"\' : \'\' ) . \' /> \';