2014-12-03 160 views
0

我試圖用一堆複選框來爲我的用戶選擇構建一個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); 

    } 

回答

0

那麼,探針......解決了......?

我跟着這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', ''); 
    } 
1

您訪問錯誤的$ _POST-keys。

相關密鑰是name-複選框的屬性,而不是id-屬性。

+0

我不知道,謝謝!但問題依然存在......我是否缺少其他東西? – Digger 2014-12-04 02:21:05

+0

什麼是'estadoDaObra' – 2014-12-04 07:02:38

+0

哦,這是另一個自定義字段,準確的下拉菜單。我是根據tuts +的教程做的。對不起,我應該鏈接它。 http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336 – Digger 2014-12-04 14:27:26