如何处理编辑页面中的复选框?

时间:2015-12-16 作者:N00b

我正在尝试4.4中引入的新术语元特性。

我在自定义分类法编辑屏幕中有很多代表特性的复选框。

我当前的设置(请注意,我使用的是正确的挂钩,我只是将它们剥离,以简化代码):

<?php
//Get previous values from db - this is edit page afterall
$feature_one = get_term_meta( $term->term_id, \'feature_one\', true );
$feature_two = get_term_meta( $term->term_id, \'feature_two\', true );
$feature_three = get_term_meta( $term->term_id, \'feature_three\', true );
//Imagine that there\'s over 20 of these
?>

<input type="checkbox" name="feature[]" id="feature-one" value="feature_one" <?php if ( $feature_one ) { echo \'checked\'; } ?> />
<input type="checkbox" name="feature[]" id="feature-two" value="feature_two" <?php if ( $feature_two ) { echo \'checked\'; } ?> />
<input type="checkbox" name="feature[]" id="feature-three" value="feature_three" <?php if ( $feature_three ) { echo \'checked\'; } ?> />
<!-- Also over 20 of these -->

<?php
//Triggers if taxonomy edit button is pressed
foreach( $_POST[\'feature\'] as $feature ) {

    if( isset( $feature ) && \'\' !== $feature ) {
        $checked_feature = $feature;
        update_term_meta( $term_id, $checked_feature, 1 );
    }
}


/* This outputs in database like this if all 3 are checked

    meta_key         meta_value
____________________________________
  feature_one      |      1
  feature_two      |      1
  feature_three    |      1

*/


/* How I need to output it in frontend - V is checked icon, X is unchecked

      |
      v

V features one    //have specific feature
X feature two     //VERY IMPORTANT to also show features that don\'t apply
V feature three   //have specific feature

*/
?>

Problems:

<如果未选中复选框,PHP不会返回任何内容。如果之前已检查功能,但未检查if edited again later, 数据库中的值未更新,请想象超过20个复选框。。我很难按照特定的顺序将它们保存到数组中,以便标签和值在前端正确配对,如果未选中的复选框返回0或其他什么,这也不会是问题,我可以创建一个选中为1、未选中为0的数组,如何解决“未选中的复选框不返回任何内容”的问题?

1 个回复
最合适的回答,由SO网友:N00b 整理而成

经过几天的试验,我终于把这个坚果弄碎了。

<!-- Note how name attribute has actual number in brackets -->

<input type="checkbox" name="feature[1]" id="feature-one" value="feature_one"<?php if ( $feature_one ) { echo \'checked\'; } ?> />
<input type="checkbox" name="feature[2]" id="feature-one" value="feature_one" <?php if ( $feature_one ) { echo \'checked\'; } ?> />

<?php

//This happens on form submit or when you press edit button
//Number or checkboxes goes here which eventually become keys
$checkboxes = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23);

$features = array();

$p = array_key_exists(\'feature\', $_POST) ? $_POST[\'feature\'] : array();

foreach( $checkboxes as $i ) {
    if(array_key_exists( $i, $p ) ) {
        //If is checked
        $features[$i] = 1;
    }
    else {
        //If is not checked
        $features[$i] = 0;
    }
}
//Save to database as an array where checked are 1 and unchecked are 0
update_term_meta( $term_id, \'features\', $features );

?>

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请