将复选框的值保存在meta框中,并在wordpress的前端(分类页而非postpage)显示。

问题描述 投票:0回答:1

我在meta box中使用这段代码创建了多个复选框。

add_action ( 'edit_category_form_fields', 'extra_category_fields');

function extra_category_fields( $tag ) {
    $t_id = $tag->term_id;
    $cat_meta = get_option( "category_$t_id");
    ?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="extra1"><?php _e('extra field'); ?></label></th>
    <td>
        <input type="text" name="Cat_meta[extra1]" id="Cat_meta[extra1]" size="25" style="width:60%;" value="<?php echo $cat_meta['extra1'] ? $cat_meta['extra1'] : ''; ?>"><br />
        <span class="description"><?php _e('extra field'); ?></span>
    </td>
</tr>

<tr class="form-field">
    <th scope="row" valign="top"><label><?php _e('extra field'); ?></label></th>
    <td>
        <input type="radio" name="radio" value="ongoing"  <?php checked( 'ongoing', get_option('radio') ); ?> >
        <label class="description" for="ongoing">ongoing</label><br>
        <input type="radio" name="radio" value="complated"  <?php checked( 'complated', get_option('radio') ); ?> >
        <label class="description" for="complated">complated</label>
    </td>
</tr>

<tr class="form-field">
    <th scope="row" valign="top"><label><?php _e('extra field'); ?></label></th>
    <td>
        <label class="checkbox-inline" for="1">
            <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 1</label>
        <label class="checkbox-inline" for="2">
            <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 2</label>
        <label class="checkbox-inline" for="3">
            <input type="checkbox" name="checkbox"<?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 3</label>
    </td>
</tr>

<?php
}
add_action ( 'edit_category_form_fields', 'extra_category_fields');

保存。

function save_extra_category_fileds( $term_id) {
    if ( isset( $_POST['Cat_meta'] ) ) {
        $t_id = $term_id;
        $cat_meta = get_option( "category_$t_id");
        $cat_keys = array_keys($_POST['Cat_meta']);

        foreach ($cat_keys as $key){
            if (isset($_POST['Cat_meta'][$key])){
                $cat_meta[$key] = $_POST['Cat_meta'][$key];
            }
            if (isset($_POST['radio'])) {
                update_option('radio', $_POST['radio']);
            }
            if (isset($_POST['checkbox'])); {
                update_option('checkbox', $_POST['checkbox']); 
            }
        }

        update_option( "category_$t_id", $cat_meta );
    }
}
add_action ( 'edited_category', 'save_extra_category_fileds');

单选选项工作正常,但当我选中其中一个复选框并刷新页面时,它又回到了未选中的状态.类别字段与普通页面不同,所以我不能使用 update_post_meta 或 $post_id。

所以,请帮助我在这......谢谢你。

wordpress checkbox categories meta-boxes
1个回答
0
投票

看起来没有任何东西可以保存你的复选框。

<td>
    <label class="checkbox-inline" for="1">
        <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 1</label>
    <label class="checkbox-inline" for="2">
        <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 2</label>
    <label class="checkbox-inline" for="3">
        <input type="checkbox" name="checkbox"<?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?>>Option 3</label>
</td>

它们没有一个 value="",所以没有什么可保存的? 你上面有3个相同的复选框,它们都没有价值。

这个应该可以。

<td>
    <label class="checkbox-inline" for="1">
        <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?> value="Option 1"> Option 1
        </label>
    <label class="checkbox-inline" for="2">
        <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?> value="Option 2"> Option 2
        </label>
    <label class="checkbox-inline" for="3">
        <input type="checkbox" name="checkbox" <?php if( 'checkbox' == true ) { ?>checked="checked"<?php } ?> value="Option 3"> Option 3
        </label>
</td>

更新

最初的问题问的是为什么复选框没有被保存下来,所以我只解决了这个问题,但是这里有你需要的其他信息... ...复选框允许多个选择,不像单选按钮只允许一个。 所以你的选择是给它们自己的名字和键值对,或者你让它们成为 checkbox 一个数组。看起来你正在将这些数据序列化,所以把它看作是数组中的数组。 在你的数据库中,键 checkbox 将被保存为'checkbox:array('选项1', '选项2', '选项3')`。

现在你的页面重载显示它们都被选中的原因是所有的复选框都在检查完全相同的值。checkbox == true - 所以基本上如果有一个,那么它们都被选中了。就像给它们不同的值是需要的,这样代码就有东西可以保存,你必须检查不同的值来决定哪一个要显示为检查。

这就是了。

<td>
    <label class="checkbox-inline" for="1">
        <input type="checkbox" name="checkbox[]" value="Option 1" <?php if( in_array( 'Option 1', $cat_meta['checkbox'] ) ) { echo 'checked'; } ?>/> Option 1
        </label>
    <label class="checkbox-inline" for="2">
        <input type="checkbox" name="checkbox[]" value="Option 2" <?php if( in_array( 'Option 2', $cat_meta['checkbox'] ) ) { echo 'checked'; } ?>> Option 2
        </label>
    <label class="checkbox-inline" for="3">
        <input type="checkbox" name="checkbox[]" value="Option 3" <?php if( in_array( 'Option 3', $cat_meta['checkbox'] ) ) { echo 'checked'; } ?>> Option 3
        </label>
</td>

你会发现现在 name=checkbox[] 末尾的方括号,表示它是一个数组,可以存储多个值。 现在检查必须验证指定的值是否等于数组中的任何一个值,所以我们使用了 if( in_array( 'Option 3', $cat_meta['checkbox'] ) ) { echo 'checked'; } 基本上说,如果这个字符串(选项3)在数组中被发现 $cat_meta[checkbox']然后呼应 "检查 "这个词。

现在已经很晚了,我已经度过了漫长的一天,但是我也在想,你可能会遇到一个问题,因为它是一个数组中的数组--但是为了解决这个问题,你只需要提取出这个数组中的 $cat_meta['checkbox'] 从整个序列化的 $cat_meta 并基本上有一个新的数组变量,如。

$cat_checkbox = $cat_meta['checkbox'];

然后你会运行你的IF语句,看看哪些复选框被选中使用。

<?php if( in_array( 'Option 2', $cat_checkbox ) ) : echo checked; endif; ?>

我从来没有完全这样做,所以我从不同的方法中应用我所知道的WP中的一般工作原理 - 所以你可能仍然需要调试一下。

© www.soinside.com 2019 - 2024. All rights reserved.