class Ludou_Tax_Image{
function __construct(){
// 新建分类页面添加自定义字段输入框
add_action( 'category_add_form_fields', array( $this, 'add_tax_image_field' ) );
// 编辑分类页面添加自定义字段输入框
add_action( 'category_edit_form_fields', array( $this, 'edit_tax_image_field' ) );
// 保存自定义字段数据
add_action( 'edited_category', array( $this, 'save_tax_meta' ), 10, 2 );
add_action( 'create_category', array( $this, 'save_tax_meta' ), 10, 2 );
} // __construct
/**
* 新建分类页面添加自定义字段输入框
*/
public function add_tax_image_field(){
?>
分类标题
输入分类标题
分类描述
输入分类描述
} // add_tax_image_field
/**
* 编辑分类页面添加自定义字段输入框
*
* @uses get_option() 从option表中获取option数据
* @uses esc_url() 确保字符串是url
*/
public function edit_tax_image_field( $term ){
// $term_id 是当前分类的id
$term_id = $term->term_id;
// 获取已保存的option
$term_meta = get_option( "ludou_taxonomy_$term_id" );
// option是一个二维数组
$biaoti = $term_meta['tax_biaoti'] ? $term_meta['tax_biaoti'] : '';
// TODO: 在这里追加获取其他自定义字段值,如获取分类描述的代码
$description = $term_meta['tax_description'] ? $term_meta['tax_description'] : '';
?>
分类标题
分类标题
分类描述
输入描述
} // edit_tax_image_field
/**
* 保存自定义字段的数据
*
* @uses get_option() 从option表中获取option数据
* @uses update_option() 更新option数据,如果没有就新建option
*/
public function save_tax_meta( $term_id ){
if ( isset( $_POST['term_meta'] ) ) {
// $term_id 是当前分类的id
$t_id = $term_id;
$term_meta = array();
$term_meta['tax_biaoti'] = isset ( $_POST['term_meta']['tax_biaoti'] ) ? $_POST['term_meta']['tax_biaoti'] : '';
// TODO: 在这里追加获取其他自定义字段表单的值,如分类描述的代码
$term_meta['tax_description'] = isset ( $_POST['term_meta']['tax_description'] ) ? $_POST['term_meta']['tax_description'] : '';
// 保存option数组
update_option( "ludou_taxonomy_$t_id", $term_meta );
} // if isset( $_POST['term_meta'] )
} // save_tax_meta
} // Ludou_Tax_Image
$wptt_tax_image = new Ludou_Tax_Image();