我试图用一堆复选框来为我的用户选择构建一个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);
}
我不知道,谢谢!但问题依然存在......我是否缺少其他东西? – 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