多个复选框metabox(wordpress)
问题描述:
我试图用一堆复选框来为我的用户选择构建一个metabox,并且我能够使它显示在我的自定义帖子类型编辑屏幕上。但复选框不保存......下面是构建复选框的代码(我认为是好的):多个复选框metabox(wordpress)
add_action('add_meta_boxes', 'adicionar_metabox');
function adicionar_metabox()
{
add_meta_box('sobreOProjeto', 'Sobre o projeto', 'projeto_callback', 'Projetos', 'normal', 'default');
}
function projeto_callback($post) {
global $post;
$valores = get_post_custom($post->ID);
$urbanidades = isset($valores['urbanidades']) ? esc_attr($valores['urbanidades'][0]) : '';
$comercial = isset($valores['comercial']) ? esc_attr($valores['comercial'][0]) : '';
$habitacao = isset($valores['habitacao']) ? esc_attr($valores['habitacao'][0]) : '';
$institucional = isset($valores['institucional']) ? esc_attr($valores['institucional'][0]) : '';
$efemero = isset($valores['efemero']) ? esc_attr($valores['efemero'][0]) : '';
$objeto = isset($valores['objeto']) ? esc_attr($valores['objeto'][0]) : '';
wp_nonce_field('my_meta_box_nonce', 'meta_box_nonce');
?>
<p>
<input type="checkbox" id="urbanidades" name="urbanidades" <?php checked($urbanidades, 'on'); ?> />
<label for="urbanidades">Urbanidades</label>
</p>
<p>
<input type="checkbox" id="comercial" name="comercial" <?php checked($comercial, 'on'); ?> />
<label for="comercial">Comercial</label>
</p>
<p>
<input type="checkbox" id="habitacao" name="habitacao" <?php checked($habitacao, 'on'); ?> />
<label for="habitacao">Habitação</label>
</p>
<p>
<input type="checkbox" id="institucional" name="institucional" <?php checked($institucional, 'on'); ?> />
<label for="institucional">Institucional</label>
</p>
<p>
<input type="checkbox" id="efemero" name="efemero" <?php checked($efemero, 'on'); ?> />
<label for="efemero">Efêmero</label>
</p>
<p>
<input type="checkbox" id="objeto" name="objeto" <?php checked($objeto, 'on'); ?> />
<label for="objeto">Objeto</label>
</p>
<?php
}
而这正是节约应该发生:
add_action('save_post', 'sobreAObra_salvar');
// This is where the saving should be taking place.
$urbanidades = isset($_POST['urbanidades']) && $_POST['estadoDaObra'] ? 'on' : 'off';
update_post_meta($post_id, 'urbanidades', $urbanidades);
$comercial = isset($_POST['comercial']) && $_POST['estadoDaObra'] ? 'on' : 'off';
update_post_meta($post_id, 'comercial', $comercial);
$habitacao = isset($_POST['habitacao']) && $_POST['estadoDaObra'] ? 'on' : 'off';
update_post_meta($post_id, 'habitacao', $habitacao);
$institucional = isset($_POST['institucional']) && $_POST['estadoDaObra'] ? 'on' : 'off';
update_post_meta($post_id, 'institucional', $institucional);
$efemero = isset($_POST['efemero']) && $_POST['estadoDaObra'] ? 'on' : 'off';
update_post_meta($post_id, 'efemero', $efemero);
$objeto = isset($_POST['objeto']) && $_POST['estadoDaObra'] ? 'on' : 'off';
update_post_meta($post_id, 'objeto', $objeto);
}
答
那么,探针......解决了......?
我跟着这tutorial如何做多个复选框。事情是,只要我不使用我以前用来表示选项的词,它就会工作。我不知道这是否是因为表中的字段已创建或别的东西,但它的工作如下:
<p>
<input type="checkbox" name="check1" id="check1" value="yes" <?php if (isset ($valores['check1'])) checked($valores['check1'][0], 'yes'); ?> />
<label for="check1">Urbanidades</label>
</p>
<p>
<input type="checkbox" name="check2" id="check2" value="yes" <?php if (isset ($valores['check2'])) checked($valores['check2'][0], 'yes'); ?> />
<label for="check2">Comercial</label>
</p>
<p>
<input type="checkbox" name="check3" id="check3" value="yes" <?php if (isset ($valores['check3'])) checked($valores['check3'][0], 'yes'); ?> />
<label for="check3">Habitação</label>
</p>
<p>
<input type="checkbox" name="check4" id="check4" value="yes" <?php if (isset ($valores['check4'])) checked($valores['check4'][0], 'yes'); ?> />
<label for="check4">Institucional</label>
</p>
<p>
<input type="checkbox" name="check5" id="check5" value="yes" <?php if (isset ($valores['check5'])) checked($valores['check5'][0], 'yes'); ?> />
<label for="check5">Efêmero</label>
</p>
<p>
<input type="checkbox" name="check6" id="check6" value="yes" <?php if (isset ($valores['check6'])) checked($valores['check6'][0], 'yes'); ?> />
<label for="check6">Objeto</label>
</p>
下面是它如何被保存。事实上,这对我来说更有意义。
if(isset($_POST[ 'check1' ])) {
update_post_meta($post_id, 'check1', 'yes');
} else {
update_post_meta($post_id, 'check1', '');
}
if(isset($_POST[ 'check2' ])) {
update_post_meta($post_id, 'check2', 'yes');
} else {
update_post_meta($post_id, 'check2', '');
}
if(isset($_POST[ 'check3' ])) {
update_post_meta($post_id, 'check3', 'yes');
} else {
update_post_meta($post_id, 'check3', '');
}
if(isset($_POST[ 'check4' ])) {
update_post_meta($post_id, 'check4', 'yes');
} else {
update_post_meta($post_id, 'check4', '');
}
if(isset($_POST[ 'check5' ])) {
update_post_meta($post_id, 'check5', 'yes');
} else {
update_post_meta($post_id, 'check5', '');
}
if(isset($_POST[ 'check6' ])) {
update_post_meta($post_id, 'check6', 'yes');
} else {
update_post_meta($post_id, 'check6', '');
}
答
您访问错误的$ _POST-keys。
相关密钥是name
-复选框的属性,而不是id
-属性。
我不知道,谢谢!但问题依然存在......我是否缺少其他东西? – Digger 2014-12-04 02:21:05
什么是'estadoDaObra' – 2014-12-04 07:02:38
哦,这是另一个自定义字段,准确的下拉菜单。我是根据tuts +的教程做的。对不起,我应该链接它。 http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336 – Digger 2014-12-04 14:27:26