这里有一个完整的示例可以帮助您开始。
注册“标记”分类法
register_taxonomy( \'marker\',
\'post\',
array(
\'hierarchical\' => false,
\'update_count_callback\' => \'_update_post_term_count\',
\'label\' => __( \'Markers\', \'textdomain\' ),
\'labels\' => array(
\'name\' => __( \'Markers\', \'textdomain\' ),
\'singular_name\' => __( \'Marker\', \'textdomain\' ),
\'menu_name\' => _x( \'Markers\', \'Admin menu name\', \'textdomain\' ),
\'search_items\' => __( \'Search Markers\', \'textdomain\' ),
\'all_items\' => __( \'All Markers\', \'textdomain\' ),
\'parent_item\' => __( \'Parent Marker\', \'textdomain\' ),
\'parent_item_colon\' => __( \'Parent Marker:\', \'textdomain\' ),
\'edit_item\' => __( \'Edit Marker\', \'textdomain\' ),
\'update_item\' => __( \'Update Marker\', \'textdomain\' ),
\'add_new_item\' => __( \'Add New Marker\', \'textdomain\' ),
\'new_item_name\' => __( \'New Marker Name\', \'textdomain\' )
),
\'rewrite\' => false,
)
);
现在我们需要添加钩子来添加/保存此分类法所需的字段
add_action( \'marker_add_form_fields\', \'marker_add_fields\', 10, 2 );
add_action( \'marker_edit_form_fields\', \'marker_edit_fields\', 10, 2 );
add_action( \'created_marker\', \'save_marker_meta\', 10, 2 );
add_action( \'edited_marker\', \'save_marker_meta\', 10, 2);
此函数添加字段以添加新分类屏幕
function marker_add_fields() {
?>
<div class="form-field">
<label for="latitude"><?php _e( \'Latitude\' ); ?></label>
<input type="text" name="latitude" id="latitude" value="">
</div>
<div class="form-field">
<label for="longitude"><?php _e( \'Longitude\' ); ?></label>
<input type="text" name="longitude" id="longitude" value="">
</div>
<?php
}
此功能将字段添加到分类法的编辑屏幕
function marker_edit_fields( $term, $taxonomy ) {
$latitude = get_term_meta( $term->term_id, \'latitude\', true );
$longitude = get_term_meta( $term->term_id, \'longitude\', true );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="latitude"><?php _e( \'Latitude\' ); ?></label></th>
<td>
<input type="text" name="latitude" id="latitude" value="<?php echo $latitude; ?>">
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="longitude"><?php _e( \'Longitude\' ); ?></label></th>
<td>
<input type="text" name="longitude" id="longitude" value="<?php echo $longitude; ?>">
</td>
</tr>
<?php
}
最后,此函数为我们的新分类法保存自定义字段值
function save_marker_meta( $term_id, $tt_id ){
if( isset( $_POST[\'latitude\'] ) && \'\' !== $_POST[\'latitude\'] ){
update_term_meta( $term_id, \'latitude\', $_POST[\'latitude\'] );
}
if( isset( $_POST[\'longitude\'] ) && \'\' !== $_POST[\'longitude\'] ){
update_term_meta( $term_id, \'longitude\', $_POST[\'longitude\'] );
}
}