// 分类添加字段
function add_taxonomy_field(){
$type = ['article' => '文章', 'product' => '产品', 'case' => '案例'];
$html = '
foreach($type as $key => $val) {
$html .= '' . $val;
}
$html .= '
echo $html;
}
add_action('category_add_form_fields','add_taxonomy_field',10,2);
// 分类编辑字段
function edit_taxonomy_field( $tag ){
$value = get_option('tag-type-'.$tag->term_id);
$type = ['article' => '文章', 'product' => '产品', 'case' => '案例'];
$html = '
类型';foreach($type as $key => $val) {
if ( $value == $key ) {
$html .= '' . $val;
} else {
$html .= '' . $val;
}
}
$html .= '
';echo $html;
}
add_action('category_edit_form_fields','edit_taxonomy_field',10,2);
// 保存数据
function save_taxonomy_field( $term_id ){
if( isset($_POST['tag-type']) ){
//判断权限--可改
if(!current_user_can('manage_categories')){
return $term_id;
}
$tag_type_key = 'tag-type-'.$term_id;
$tag_type_value = $_POST['tag-type'];
// 更新选项值
update_option( $tag_type_key, $tag_type_value );
}
}
function taxonomy_setup() {
add_action('created_category','save_taxonomy_field',10,1);
add_action('edited_category','save_taxonomy_field',10,1);
}
add_action( 'admin_init', 'taxonomy_setup' );