【问题标题】:WordPress: add check-boxes to custom taxonomy add/edit formWordPress:将复选框添加到自定义分类添加/编辑表单
【发布时间】:2016-08-12 08:45:03
【问题描述】:

我需要在自定义分类添加/编辑表单中添加一个复选框列表。 我有这段代码用于在我的插件中为自定义分类表单添加一个文本字段,它工作正常:

    <?php
    function taxonomy_edit_meta_field($term) {
        $t_id = $term->term_id;
        $term_meta = get_option( "taxonomy_$t_id" ); 
    ?>

        <tr class="form-field">
            <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Term:' ); ?></label></th>
            <td>
                <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
            </td>
        </tr>

    <?php
    }
    add_action( 'product_cat_edit_form_fields', 'taxonomy_edit_meta_field', 10, 2 );

    function save_taxonomy_custom_meta( $term_id ) {
        if ( isset( $_POST['term_meta'] ) ) {
            $t_id = $term_id;
            $term_meta = get_option( "taxonomy_$t_id" );
            $cat_keys = array_keys( $_POST['term_meta'] );
            foreach ( $cat_keys as $key ) {
                if ( isset ( $_POST['term_meta'][$key] ) ) {
                    $term_meta[$key] = $_POST['term_meta'][$key];
                }
            }
            update_option( "taxonomy_$t_id", $term_meta );
        }
    }  
    add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );  
    add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );

如何以相同的方式添加复选框列表?

【问题讨论】:

    标签: php html wordpress checkbox custom-taxonomy


    【解决方案1】:

    您需要更改挂钩的名称。

     add_action( 'edited_custom_tax', 'save_taxonomy_custom_meta', 10, 2 );
    

    【讨论】:

    • 我应该将哪些属性值添加到复选框输入标签(名称、值)?如何保存选中的字段?如何在编辑页面上显示复选框?
    【解决方案2】:

    保存未选中复选框输入有问题。 我已经用 input type="hidden" 修复了它,它的 "name" 属性值与复选框输入中的值相同。

        <tr class="form-field">
            <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Color:' ); ?></label></th>
            <td>
                <input type="hidden" value="0" name="term_meta[pa_color_attr]">
                <input type="checkbox" <?php echo (!empty($term_meta['pa_color_attr']) ? ' checked="checked" ' : 'test'); ?> value="1" name="term_meta[pa_color_attr]" />
            </td>
        </tr>
    

    【讨论】:

      猜你喜欢
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-15
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 2019-07-04
      相关资源
      最近更新 更多