我有一个为分类术语添加术语元的工作方法。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...>
并且能够保存、更新每个选定的多个值等?此外,它们是否可以显示在列中?
提前谢谢。