WordPress 分类添加自定义字段

在页面中,常常需要对分类添加各种字段,使用下面方法,可以添加自定义字段。字段类型可以是text或者radio各种类型。

 

/* ------------------------------------------------------------------------------------ */
// 分类添加字段
function ems_add_category_field(){
	echo '<div class="form-field">
		<label for="cat-keywords">关键词</label>
		<input name="cat-keywords" id="cat-keywords" type="text" value="" size="40">
		<p>输入关键词</p>
		</div>';
}
add_action('category_add_form_fields','ems_add_category_field',10,2);

// 编辑分类字段
function ems_edit_category_field($tag){
	echo '<tr class="form-field">
		<th scope="row"><label for="cat-keywords">关键词</label></th>
		<td>
		<input name="cat-keywords" id="cat-keywords" type="text" value="';
	echo get_option('cat-keywords-'.$tag->term_id).'" size="40"/><br>
		<p class="description">'.$tag->name.' 关键词</p>
		</td>
		</tr>';
}
add_action('category_edit_form_fields','ems_edit_category_field',10,2);

// 保存数据
function ems_taxonomy_metadate($term_id)
{
	if (isset($_POST['cat-keywords'])) {
		//判断权限--可改
		if (!current_user_can('manage_categories')) {
			return $term_id;
		}
		$cat_key = 'cat-keywords-' . $term_id; // key 选项名为 cat-keywords-1 类型
		$cat_value = $_POST['cat-keywords'];	// value
		// 更新选项值
		update_option($cat_key, $cat_value);
	}
}

add_action('created_category','ems_taxonomy_metadate',10,1);
add_action('edited_category','ems_taxonomy_metadate',10,1);
收藏 0
评论
WordPress 主题开发
WordPress 添加 Favicon 图标的方法
2021-06-23 11:43:08
WordPress 基础
WordPress 设置菜单
2021-06-25 16:45:48
生成中...
登录
注册
重置密码