术语元-使用选择选项将多个值保存在一个表单中

时间:2017-09-27 作者:Karls

我有一个为分类术语添加术语元的工作方法。CPT是\'houses\' 分类法是\'house_feature\'. 这是我的代码。。。

首先,数组中的可能值:

$feature_groups = array(
    \'bedroom\'   =>  __( \'Bedroom\', \'genesis\' ),
    \'living\'    =>  __( \'Living Room\', \'genesis\' ),
    \'kitchen\'   =>  __( \'Kitchen\', \'genesis\' )
);
然后,使用{$taxonomy}\\u add\\u Form\\u字段挂钩,扩展WordPress术语编辑表单:

add_action( \'house_feature_add_form_fields\', \'add_feature_group_field\', 10, 2 );
function add_feature_group_field( $taxonomy ) {

    global $feature_groups;

    ?>
    <div class="form-field term-group">
        <label for="feature-group"><?php _e( \'Feature Group\', \'genesis\' ); ?></label>
        <select class="postform" id="equipment-group" name="feature-group">
            <option value="-1"><?php _e( \'none\', \'genesis\' ) ?></option>
        <?php foreach ( $feature_groups as $_group_key => $_group ) : ?>

            <option value="<?php echo $_group_key; ?>" class=""><?php echo $_group; ?></option>

        <?php endforeach; ?>
        </select>
    </div>
    <?php

}
然后,使用created\\u{$taxonomy}钩子保存术语Meta,因此它是“created\\uhouse\\u feature”:

add_action( \'created_house_feature\', \'save_feature_meta\', 10, 2 );
function save_feature_meta( $term_id, $tt_id ) {

    if ( !empty( $_POST[\'feature-group\'] ) ) {

        $group = sanitize_title( $_POST[\'feature-group\'] );
        add_term_meta( $term_id, \'feature-group\', $group, true );

    }

}
使用{$taxonomy}\\u edit\\u form\\u fields钩子的更新例程将组的字段获取到编辑表单中:

add_action( \'house_feature_edit_form_fields\', \'edit_feature_group_field\', 10, 2 );
function edit_feature_group_field( $term, $taxonomy ) {

    global $feature_groups;

    // Get current group.
    $feature_group = get_term_meta( $term->term_id, \'feature-group\', true );

    ?>
    <tr class="form-field term-group-wrap">
        <th scope="row"><label for="feature-group"><?php _e( \'Feature Group\', \'genesis\' ); ?></label></th>
        <td>
            <select class="postform" id="feature-group" name="feature-group">
                <option value="-1"><?php _e( \'none\', \'genesis\' ); ?></option>
                <?php foreach ( $feature_groups as $_group_key => $_group ) : ?>

                    <option value="<?php $_group_key; ?>" <?php selected( $feature_group, $_group_key ); ?>><?php echo $_group; ?></option>

                <?php endforeach; ?>

            </select>
        </td>
    </tr>
    <?php

}
将编辑后的数据保存到edited\\{$taxonomy}:

add_action( \'edited_house_feature\', \'update_feature_meta\', 10, 2 );
function update_feature_meta( $term_id, $tt_id ) {

    if ( !empty( $_POST[\'feature-group\'] ) ) {

        $group = sanitize_title( $_POST[\'feature-group\'] );

        update_term_meta( $term_id, \'feature-group\', $group );

    }

}
在术语列表表的新列中显示术语元数据:

add_filter( \'manage_edit-house_feature_columns\', \'add_feature_group_column\' );
function add_feature_group_column( $columns ) {

    $columns[\'feature_group\'] = __( \'Group\', \'genesis\' );

    return $columns;

}
将内容添加到列字段中,我们可以使用钩子模式manage\\uu{$taxonomy}\\u custom\\u列:

add_filter( \'manage_house_feature_custom_column\', \'add_feature_group_column_content\', 10, 3 );
function add_feature_group_column_content( $content, $column_name, $term_id ) {

    global $feature_groups;

    if ( $column_name !== \'feature_group\' ) {
        return $content;
    }

    $term_id        = absint( $term_id );
    $feature_group  = get_term_meta( $term_id, \'feature-group\', true );

    if ( ! empty( $feature_group ) ) {
        $content .= esc_attr( $feature_groups[ $feature_group ] );
    }

    return $content;

}
使组列可排序:

add_filter( \'manage_edit-house_feature_sortable_columns\', \'add_feature_group_column_sortable\' );
function add_feature_group_column_sortable( $sortable ) {

    $sortable[\'feature_group\'] = \'feature_group\';

    return $sortable;

}
好的,所有这些都来自一个教程,它工作得很好。此代码仅创建、编辑、保存和删除一个您可以选择的值。我希望能够在multiselect中选择多个值。我的问题是:我怎样才能<select multiple...> 并且能够保存、更新每个选定的多个值等?此外,它们是否可以显示在列中?

提前谢谢。

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

我找到了解决方案,所有的功劳都归功于@gmazzap,他在这篇帖子中给出了这个很好的答案:How do I save each option in a multiple select menu as it\'s own meta_key + meta_value pair?

add_action( \'created_house_feature\', \'save_house_feature_meta\', 10, 2 );
function save_house_feature_meta( $term_id, $tt_id ) {

    $old_feature_group = get_term_meta( $term->term_id, \'feature-group, true\' );
    $new_feature_groups = isset( $_POST[\'feature-group\'] ) ? $_POST[\'feature-group\'] : array();

    if ( empty( $new_feature_group ) ) {

        // No Feature Groups selected. Then, completely delete all Meta values for the Term.
        delete_term_meta( $term_id, \'feature-group\' );

    } else {

        $already_feature_group = array();

        if ( ! empty( $old_feature_group ) ) {

            foreach ( $old_feature_group as $value_feature_group ) {

                if ( ! in_array( $value_feature_group, $new_feature_group ) ) {

                    // This value was selected, but now it isn\'t, so delete it.
                    delete_term_meta( $term_id, \'feature-group\', $value_feature_group );

                } else {

                    // This value is already saved, we can skip it from saving.
                    $already_feature_group[] = $value_feature_group;

                }

            }

        }

        // We don\'t save what is already saved.
        $to_save_feature_group = array_diff( $new_feature_group, $already_feature_group );

        if ( ! empty( $to_save_feature_group ) ) {

            foreach ( $to_save_feature_group as $feature_group ) {

                add_term_meta( $term_id, \'feature-group\', $feature_group, false );

            }

        }

    }

}

结束

相关推荐

Theme Loading Into Dashboard

我有一个很奇怪的,以前从未见过的问题。。。每当我访问仪表板时,大约3-5秒后,我的主题就会加载到仪表板布局中。这就像主题的样式表以某种方式排队。。。这是我的排队钩functions.php:function myyogawebsite_scripts() { wp_enqueue_style( \'foundation-normalize\', get_template_directory_uri() . \'/css/normalize.css\' ); wp_