在这篇教程作者将要结束给WordPress分类添加字段的教程,教程最后当然是送出类文件,前面也放出过两个类文件:主题后台、文章自定义字段。
但是这个类文件不一样,该类文件为阿树工作室原创,所以有不完美的地方,比如没有提供图片上传的,不过如果读者需要,参考一下前面主题后台和文章自定义字段的两个类文件,完全可以自己添加进来。
版本控制:
2013.07.08,版本 1.0
1、增加删除分类删除数据功能
2、增加和更新-编辑器、图片上传、复选框等功能
类文件代码-2013年7月8日更新:
<?php /* wordpress分类法自定义字段类文件 Version: 1.0 Author: 树是我的朋友 Author URI: Http://www.dba.cn License: /courses/highgrade/313.html */ class ashu_taxonomy_feild{ var $options; var $taxonomyinfo; function ashu_taxonomy_feild($options,$taxonomyinfo){ $this->options = $options; $this->taxonomyinfo = $taxonomyinfo; foreach($this->taxonomyinfo as $taxonomy){ add_action($taxonomy.'_add_form_fields', array(&$this, 'taxonomy_fields_adds'), 10, 2); add_action($taxonomy.'_edit_form_fields', array(&$this, 'taxonomy_metabox_edit'), 10, 2); add_action('created_'.$taxonomy, array(&$this, 'save_taxonomy_metadata'), 10, 1); add_action('edited_'.$taxonomy,array(&$this, 'save_taxonomy_metadata'), 10, 1); add_action('admin_menu', array(&$this, 'init_taxonomy')); add_action('delete_'.$taxonomy, array(&$this,'delete_taxonomy_metadata'),10,1); } } function init_taxonomy(){ if(basename( $_SERVER['PHP_SELF']) == "edit-tags.php") { //注意加载的脚本的url wp_enqueue_style('cate_fields_css', TEMJS_URI. 'cate_fields.css'); wp_enqueue_script('cate_fields_js',TEMJS_URI. 'cate_fields.js'); wp_enqueue_script('thickbox'); wp_enqueue_style('thickbox'); } } /*********添加分类页面*******/ function taxonomy_fields_adds($tag){ foreach($this->options as $option){ if( (!isset($option["edit_only"]) || !$option["edit_only"] ) ){ if (method_exists($this, $option['type'])){ echo '<div class="form-field">'; echo '<label for="'.$option['id'].'" >'.$option['name'].'</label>'; $this->$option['type']($option); echo '</div>'; } } } } /*************编辑分类页面*****************/ function taxonomy_metabox_edit($tag){ foreach($this->options as $option){ if (method_exists($this, $option['type'])){ if(get_term_meta($tag->term_id , $option['id']) !== ""){ $option['std'] = get_term_meta($tag->term_id,$option['id'], true); } echo '<tr class="form-field">'; echo '<th scope="row" valign="top">'; echo '<label for="'.$option['id'].'" >'.$option['name'].'</label>'; echo '</th>'; echo '<td>'; $this->$option['type']($option); echo '</td>'; echo '</tr>'; } } } /**********删除数据******/ function delete_taxonomy_metadata($term_id){ foreach($this->options as $option){ delete_term_meta($term_id,$option['id']); } } /**************保存数据*****************/ function save_taxonomy_metadata($term_id){ foreach($this->options as $option){ if(isset($_POST[$option['id']])){ if(!current_user_can('manage_categories')){ return $term_id ; } if( $option['type'] == 'tinymce' ){ $data = stripslashes($_POST[$option['id']]); }elseif( $option['type'] == 'checkbox' ){ $data = $_POST[$option['id']]; }else{ $data = htmlspecialchars($_POST[$option['id']], ENT_QUOTES,"UTF-8"); } if(get_term_meta($term_id , $option['id']) == "") add_term_meta($term_id , $option['id'], $data, true); elseif($data != get_term_meta($term_id , $option['id'], true)) update_term_meta($term_id , $option['id'], $data); elseif($data == "") delete_term_meta($term_id , $option['id'], get_term_meta($term_id , $option['id'], true)); } } } /******提示信息******/ function info($option){ echo '<p>'.$option['desc'].'</p>'; } /*******文本输入框text*******/ function text($option){ echo '<input type="text" size="'.$option['size'].'" value="'; echo $option['std']; echo '" id="'.$option['id'].'" name="'.$option['id'].'"/>'; echo '<p>'.$option['desc'].'</p>'; } /******文本域***********/ function textarea($option){ echo '<textarea cols="40" rows="5" id="'.$option['id'].'" name="'.$option['id'].'">'.$option['std'].'</textarea>'; echo '<p>'.$option['desc'].'</p>'; } /**********单选框************/ function radio( $option ){ foreach( $option['buttons'] as $key=>$value ) { $checked =""; if( $option['std'] == $key) { $checked = 'checked = "checked"'; } echo '<input '.$checked.' type="radio" class="kcheck" value="'.$key.'" name="'.$option['id'].'"/>'.$value; } } /**************复选框*******************/ function checkbox($option) { foreach( $option['buttons'] as $key=>$value ) { $checked =""; if( is_array($option['std']) && in_array($key,$option['std'])) { $checked = 'checked = "checked"'; } echo '<input '.$checked.' type="checkbox" class="kcheck" value="'.$key.'" name="'.$option['id'].'[]"/>'.$value; } } /***********下拉选择***********/ function dropdown($option){ if($option['subtype'] == 'page') { $select = 'Select page'; $entries = get_pages('title_li=&orderby=name'); }else if($option['subtype'] == 'sidebar'){ global $wp_registered_sidebars; $select = 'Select a special sidebar'; $entries = $wp_registered_sidebars; }else if($option['subtype'] == 'cat'){ $select = 'Select category'; $entries = get_categories('title_li=&orderby=name&hide_empty=0'); }else{ $select = 'Select...'; $entries = $option['subtype']; } echo '<select class="postform" id="'. $option['id'] .'" name="'. $option['id'] .'"> '; echo '<option value="">请选择...</option> '; foreach ($entries as $key => $entry){ if($option['subtype'] == 'page'){ $id = $entry->ID; $title = $entry->post_title; }else if($option['subtype'] == 'cat'){ $id = $entry->term_id; $title = $entry->name; }else if($option['subtype'] == 'sidebar'){ $id = $entry['id']; $title = $entry['name']; }else{ $id = $key; $title = $entry; } $selected=''; if( $option['std'] == $id ){ $selected = "selected='selected'"; } echo"<option $selected value='". $id ."'>". $title."</option>"; } echo '</select>'; echo '<p>'.$option['desc'].'</p>'; } /***************图片上传**********************/ function upload($option){ $previmg = ''; if($option['std'] != ''){$prevImg = '<img src='.$option['std'].' alt="" />';} echo '<div class="preview_pic_optionspage" id="'.$option['id'].'_div">'.$prevImg.'</div>'; echo $option['desc'].'<br/>'; echo '<input type="text" size="60" value="'.$option['std'].'" name="'.$option['id'].'" class="upload_pic_input" />'; echo ' <a onclick="return false;" title="" class="cat_up button thickbox" id="'.$option['id'].'" href="media-upload.php?type=image&TB_iframe=true">Insert Image</a>'; } /*******************编辑器****************************/ function tinymce($option){ wp_editor( $option['std'], $option['id'],$settings=array('tinymce'=>1,'media_buttons'=>0,) ); } } ?> $options = array(); $taxonomyinfo = array('category'); $options[] = array( "id" => "_notice", "desc" => '提示信息内容', "type" => "info"); $options[] = array( "name" => "分类关键词", "desc" => "", "id" => "_seo_keywords", "std" => "", "edit_only"=>false, "size" => 40, "type" => "text"); $options[] = array( "name" => "文本域测试", "desc" => "描述信息", "id" => "_textarea", "std" => "", "edit_only"=>false, "size" => 40, "type" => "textarea"); $options[] = array( "name" => "单选框测试", "desc" => "请选择性别", "id" => "_radio", "std" => "sprother", "edit_only"=>false, "buttons" => array( 'man'=>'男性', 'woman'=>'女性', 'sprother'=>'中性' ), "type" => "radio"); $options[] = array( "name" => "复选框--你喜欢哪几个", "id" => "_checkbox", "std" => "", "buttons" => array( 'chun'=>'春哥', 'zeng'=>'曾哥', 'zhu'=>'著姐', 'feng'=>'玉凤', 'fu'=>'芙蓉' ), "edit_only"=>false, "type" => "checkbox"); $options[] = array( "name" => "下拉框框测试-页面", "desc" => "描述信息", "id" => "_drop_page", "std" => "", "edit_only"=>false, "subtype" => 'page', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-分类", "desc" => "描述信息", "id" => "_drop_cat", "std" => "", "edit_only"=>false, "subtype" => 'cat', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-侧边栏", "desc" => "描述信息", "id" => "_drop_sidebar", "std" => "", "edit_only"=>false, "subtype" => 'sidebar', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-自定义", "desc" => "描述信息", "id" => "_drop_custom", "std" => "", "edit_only"=>false, "subtype" => array( 'apple'=>'苹果', 'banana'=>'香蕉', 'orange'=>'桔子', ), "type" => "dropdown"); $options[] = array( "name" => "上传图片测试", "desc" => "", "id" => "_upload", "std" => "", "edit_only"=>true, "type" => "upload" ); $options[] = array( "name" => "tinymce编辑器测试", "desc" => "", "id" => "_tinymce", "std" => "", "edit_only"=>true, "type" => "tinymce" ); $new_taxonomy_feild = new ashu_taxonomy_feild($options, $taxonomyinfo); include_once(get_template_directory() .'/include/class-taxonomy-feild.php'); include_once(get_template_directory() .'/include/simple-term-meta.php'); $_seo_keywords = get_term_meta( $cat->term_id, '_seo_keywords',true );
下面提供类文件下载(共两个文件):wordpress给分类添加字段类文件其中一个类文件,一个simp-term-meta.PHP文件
配置示例:
注意:
- 其中配置数组中的edit_only参数如果设置为true,则该项将只会在分类的编辑页面出现,主要考虑是,在分类的添加页面,编辑器或者图片上传可能太大,所以一般将编辑器和图片上传设置为只在编辑页面出现。
- 复选框保存的数据为数组
<?php /* wordpress分类法自定义字段类文件 Version: 1.0 Author: 树是我的朋友 Author URI: http://www.dba.cn License: /courses/highgrade/313.html */ class ashu_taxonomy_feild{ var $options; var $taxonomyinfo; function ashu_taxonomy_feild($options,$taxonomyinfo){ $this->options = $options; $this->taxonomyinfo = $taxonomyinfo; foreach($this->taxonomyinfo as $taxonomy){ add_action($taxonomy.'_add_form_fields', array(&$this, 'taxonomy_fields_adds'), 10, 2); add_action($taxonomy.'_edit_form_fields', array(&$this, 'taxonomy_metabox_edit'), 10, 2); add_action('created_'.$taxonomy, array(&$this, 'save_taxonomy_metadata'), 10, 1); add_action('edited_'.$taxonomy,array(&$this, 'save_taxonomy_metadata'), 10, 1); add_action('admin_menu', array(&$this, 'init_taxonomy')); add_action('delete_'.$taxonomy, array(&$this,'delete_taxonomy_metadata'),10,1); } } function init_taxonomy(){ if(basename( $_SERVER['PHP_SELF']) == "edit-tags.php") { //注意加载的脚本的url wp_enqueue_style('cate_fields_css', TEMJS_URI. 'cate_fields.css'); wp_enqueue_script('cate_fields_js',TEMJS_URI. 'cate_fields.js'); wp_enqueue_script('thickbox'); wp_enqueue_style('thickbox'); } } /*********添加分类页面*******/ function taxonomy_fields_adds($tag){ foreach($this->options as $option){ if( (!isset($option["edit_only"]) || !$option["edit_only"] ) ){ if (method_exists($this, $option['type'])){ echo '<div class="form-field">'; echo '<label for="'.$option['id'].'" >'.$option['name'].'</label>'; $this->$option['type']($option); echo '</div>'; } } } } /*************编辑分类页面*****************/ function taxonomy_metabox_edit($tag){ foreach($this->options as $option){ if (method_exists($this, $option['type'])){ if(get_term_meta($tag->term_id , $option['id']) !== ""){ $option['std'] = get_term_meta($tag->term_id,$option['id'], true); } echo '<tr class="form-field">'; echo '<th scope="row" valign="top">'; echo '<label for="'.$option['id'].'" >'.$option['name'].'</label>'; echo '</th>'; echo '<td>'; $this->$option['type']($option); echo '</td>'; echo '</tr>'; } } } /**********删除数据******/ function delete_taxonomy_metadata($term_id){ foreach($this->options as $option){ delete_term_meta($term_id,$option['id']); } } /**************保存数据*****************/ function save_taxonomy_metadata($term_id){ foreach($this->options as $option){ if(isset($_POST[$option['id']])){ if(!current_user_can('manage_categories')){ return $term_id ; } if( $option['type'] == 'tinymce' ){ $data = stripslashes($_POST[$option['id']]); }elseif( $option['type'] == 'checkbox' ){ $data = $_POST[$option['id']]; }else{ $data = htmlspecialchars($_POST[$option['id']], ENT_QUOTES,"UTF-8"); } if(get_term_meta($term_id , $option['id']) == "") add_term_meta($term_id , $option['id'], $data, true); elseif($data != get_term_meta($term_id , $option['id'], true)) update_term_meta($term_id , $option['id'], $data); elseif($data == "") delete_term_meta($term_id , $option['id'], get_term_meta($term_id , $option['id'], true)); } } } /******提示信息******/ function info($option){ echo '<p>'.$option['desc'].'</p>'; } /*******文本输入框text*******/ function text($option){ echo '<input type="text" size="'.$option['size'].'" value="'; echo $option['std']; echo '" id="'.$option['id'].'" name="'.$option['id'].'"/>'; echo '<p>'.$option['desc'].'</p>'; } /******文本域***********/ function textarea($option){ echo '<textarea cols="40" rows="5" id="'.$option['id'].'" name="'.$option['id'].'">'.$option['std'].'</textarea>'; echo '<p>'.$option['desc'].'</p>'; } /**********单选框************/ function radio( $option ){ foreach( $option['buttons'] as $key=>$value ) { $checked =""; if( $option['std'] == $key) { $checked = 'checked = "checked"'; } echo '<input '.$checked.' type="radio" class="kcheck" value="'.$key.'" name="'.$option['id'].'"/>'.$value; } } /**************复选框*******************/ function checkbox($option) { foreach( $option['buttons'] as $key=>$value ) { $checked =""; if( is_array($option['std']) && in_array($key,$option['std'])) { $checked = 'checked = "checked"'; } echo '<input '.$checked.' type="checkbox" class="kcheck" value="'.$key.'" name="'.$option['id'].'[]"/>'.$value; } } /***********下拉选择***********/ function dropdown($option){ if($option['subtype'] == 'page') { $select = 'Select page'; $entries = get_pages('title_li=&orderby=name'); }else if($option['subtype'] == 'sidebar'){ global $wp_registered_sidebars; $select = 'Select a special sidebar'; $entries = $wp_registered_sidebars; }else if($option['subtype'] == 'cat'){ $select = 'Select category'; $entries = get_categories('title_li=&orderby=name&hide_empty=0'); }else{ $select = 'Select...'; $entries = $option['subtype']; } echo '<select class="postform" id="'. $option['id'] .'" name="'. $option['id'] .'"> '; echo '<option value="">请选择...</option> '; foreach ($entries as $key => $entry){ if($option['subtype'] == 'page'){ $id = $entry->ID; $title = $entry->post_title; }else if($option['subtype'] == 'cat'){ $id = $entry->term_id; $title = $entry->name; }else if($option['subtype'] == 'sidebar'){ $id = $entry['id']; $title = $entry['name']; }else{ $id = $key; $title = $entry; } $selected=''; if( $option['std'] == $id ){ $selected = "selected='selected'"; } echo"<option $selected value='". $id ."'>". $title."</option>"; } echo '</select>'; echo '<p>'.$option['desc'].'</p>'; } /***************图片上传**********************/ function upload($option){ $prevImg = ''; if($option['std'] != ''){$prevImg = '<img src='.$option['std'].' alt="" />';} echo '<div class="preview_pic_optionspage" id="'.$option['id'].'_div">'.$prevImg.'</div>'; echo $option['desc'].'<br/>'; echo '<input type="text" size="60" value="'.$option['std'].'" name="'.$option['id'].'" class="upload_pic_input" />'; echo ' <a onclick="return false;" title="" class="cat_up button thickbox" id="'.$option['id'].'" href="media-upload.php?type=image&TB_iframe=true">Insert Image</a>'; } /*******************编辑器****************************/ function tinymce($option){ wp_editor( $option['std'], $option['id'],$settings=array('tinymce'=>1,'media_buttons'=>0,) ); } } ?> $options = array(); $taxonomyinfo = array('category'); $options[] = array( "id" => "_notice", "desc" => '提示信息内容', "type" => "info"); $options[] = array( "name" => "分类关键词", "desc" => "", "id" => "_seo_keywords", "std" => "", "edit_only"=>false, "size" => 40, "type" => "text"); $options[] = array( "name" => "文本域测试", "desc" => "描述信息", "id" => "_textarea", "std" => "", "edit_only"=>false, "size" => 40, "type" => "textarea"); $options[] = array( "name" => "单选框测试", "desc" => "请选择性别", "id" => "_radio", "std" => "sprother", "edit_only"=>false, "buttons" => array( 'man'=>'男性', 'woman'=>'女性', 'sprother'=>'中性' ), "type" => "radio"); $options[] = array( "name" => "复选框--你喜欢哪几个", "id" => "_checkbox", "std" => "", "buttons" => array( 'chun'=>'春哥', 'zeng'=>'曾哥', 'zhu'=>'著姐', 'feng'=>'玉凤', 'fu'=>'芙蓉' ), "edit_only"=>false, "type" => "checkbox"); $options[] = array( "name" => "下拉框框测试-页面", "desc" => "描述信息", "id" => "_drop_page", "std" => "", "edit_only"=>false, "subtype" => 'page', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-分类", "desc" => "描述信息", "id" => "_drop_cat", "std" => "", "edit_only"=>false, "subtype" => 'cat', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-侧边栏", "desc" => "描述信息", "id" => "_drop_sidebar", "std" => "", "edit_only"=>false, "subtype" => 'sidebar', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-自定义", "desc" => "描述信息", "id" => "_drop_custom", "std" => "", "edit_only"=>false, "subtype" => array( 'apple'=>'苹果', 'banana'=>'香蕉', 'orange'=>'桔子', ), "type" => "dropdown"); $options[] = array( "name" => "上传图片测试", "desc" => "", "id" => "_upload", "std" => "", "edit_only"=>true, "type" => "upload" ); $options[] = array( "name" => "tinymce编辑器测试", "desc" => "", "id" => "_tinymce", "std" => "", "edit_only"=>true, "type" => "tinymce" ); $new_taxonomy_feild = new ashu_taxonomy_feild($options, $taxonomyinfo); include_once(get_template_directory() .'/include/class-taxonomy-feild.php'); include_once(get_template_directory() .'/include/simple-term-meta.php'); $_seo_keywords = get_term_meta( $cat->term_id, '_seo_keywords',true );
使用方法:
1、解压之后得到两个文件,在主题中包含这两个文件,分别是文件和simple-term-meta.php文件。
2、在你的主题中包含这两个文件这两个文件(如阿树将文件放在主题的include文件夹中,则使用如下代码):
<?php /* wordpress分类法自定义字段类文件 Version: 1.0 Author: 树是我的朋友 Author URI: http://www.dba.cn License: /courses/highgrade/313.html */ class ashu_taxonomy_feild{ var $options; var $taxonomyinfo; function ashu_taxonomy_feild($options,$taxonomyinfo){ $this->options = $options; $this->taxonomyinfo = $taxonomyinfo; foreach($this->taxonomyinfo as $taxonomy){ add_action($taxonomy.'_add_form_fields', array(&$this, 'taxonomy_fields_adds'), 10, 2); add_action($taxonomy.'_edit_form_fields', array(&$this, 'taxonomy_metabox_edit'), 10, 2); add_action('created_'.$taxonomy, array(&$this, 'save_taxonomy_metadata'), 10, 1); add_action('edited_'.$taxonomy,array(&$this, 'save_taxonomy_metadata'), 10, 1); add_action('admin_menu', array(&$this, 'init_taxonomy')); add_action('delete_'.$taxonomy, array(&$this,'delete_taxonomy_metadata'),10,1); } } function init_taxonomy(){ if(basename( $_SERVER['PHP_SELF']) == "edit-tags.php") { //注意加载的脚本的url wp_enqueue_style('cate_fields_css', TEMJS_URI. 'cate_fields.css'); wp_enqueue_script('cate_fields_js',TEMJS_URI. 'cate_fields.js'); wp_enqueue_script('thickbox'); wp_enqueue_style('thickbox'); } } /*********添加分类页面*******/ function taxonomy_fields_adds($tag){ foreach($this->options as $option){ if( (!isset($option["edit_only"]) || !$option["edit_only"] ) ){ if (method_exists($this, $option['type'])){ echo '<div class="form-field">'; echo '<label for="'.$option['id'].'" >'.$option['name'].'</label>'; $this->$option['type']($option); echo '</div>'; } } } } /*************编辑分类页面*****************/ function taxonomy_metabox_edit($tag){ foreach($this->options as $option){ if (method_exists($this, $option['type'])){ if(get_term_meta($tag->term_id , $option['id']) !== ""){ $option['std'] = get_term_meta($tag->term_id,$option['id'], true); } echo '<tr class="form-field">'; echo '<th scope="row" valign="top">'; echo '<label for="'.$option['id'].'" >'.$option['name'].'</label>'; echo '</th>'; echo '<td>'; $this->$option['type']($option); echo '</td>'; echo '</tr>'; } } } /**********删除数据******/ function delete_taxonomy_metadata($term_id){ foreach($this->options as $option){ delete_term_meta($term_id,$option['id']); } } /**************保存数据*****************/ function save_taxonomy_metadata($term_id){ foreach($this->options as $option){ if(isset($_POST[$option['id']])){ if(!current_user_can('manage_categories')){ return $term_id ; } if( $option['type'] == 'tinymce' ){ $data = stripslashes($_POST[$option['id']]); }elseif( $option['type'] == 'checkbox' ){ $data = $_POST[$option['id']]; }else{ $data = htmlspecialchars($_POST[$option['id']], ENT_QUOTES,"UTF-8"); } if(get_term_meta($term_id , $option['id']) == "") add_term_meta($term_id , $option['id'], $data, true); elseif($data != get_term_meta($term_id , $option['id'], true)) update_term_meta($term_id , $option['id'], $data); elseif($data == "") delete_term_meta($term_id , $option['id'], get_term_meta($term_id , $option['id'], true)); } } } /******提示信息******/ function info($option){ echo '<p>'.$option['desc'].'</p>'; } /*******文本输入框text*******/ function text($option){ echo '<input type="text" size="'.$option['size'].'" value="'; echo $option['std']; echo '" id="'.$option['id'].'" name="'.$option['id'].'"/>'; echo '<p>'.$option['desc'].'</p>'; } /******文本域***********/ function textarea($option){ echo '<textarea cols="40" rows="5" id="'.$option['id'].'" name="'.$option['id'].'">'.$option['std'].'</textarea>'; echo '<p>'.$option['desc'].'</p>'; } /**********单选框************/ function radio( $option ){ foreach( $option['buttons'] as $key=>$value ) { $checked =""; if( $option['std'] == $key) { $checked = 'checked = "checked"'; } echo '<input '.$checked.' type="radio" class="kcheck" value="'.$key.'" name="'.$option['id'].'"/>'.$value; } } /**************复选框*******************/ function checkbox($option) { foreach( $option['buttons'] as $key=>$value ) { $checked =""; if( is_array($option['std']) && in_array($key,$option['std'])) { $checked = 'checked = "checked"'; } echo '<input '.$checked.' type="checkbox" class="kcheck" value="'.$key.'" name="'.$option['id'].'[]"/>'.$value; } } /***********下拉选择***********/ function dropdown($option){ if($option['subtype'] == 'page') { $select = 'Select page'; $entries = get_pages('title_li=&orderby=name'); }else if($option['subtype'] == 'sidebar'){ global $wp_registered_sidebars; $select = 'Select a special sidebar'; $entries = $wp_registered_sidebars; }else if($option['subtype'] == 'cat'){ $select = 'Select category'; $entries = get_categories('title_li=&orderby=name&hide_empty=0'); }else{ $select = 'Select...'; $entries = $option['subtype']; } echo '<select class="postform" id="'. $option['id'] .'" name="'. $option['id'] .'"> '; echo '<option value="">请选择...</option> '; foreach ($entries as $key => $entry){ if($option['subtype'] == 'page'){ $id = $entry->ID; $title = $entry->post_title; }else if($option['subtype'] == 'cat'){ $id = $entry->term_id; $title = $entry->name; }else if($option['subtype'] == 'sidebar'){ $id = $entry['id']; $title = $entry['name']; }else{ $id = $key; $title = $entry; } $selected=''; if( $option['std'] == $id ){ $selected = "selected='selected'"; } echo"<option $selected value='". $id ."'>". $title."</option>"; } echo '</select>'; echo '<p>'.$option['desc'].'</p>'; } /***************图片上传**********************/ function upload($option){ $prevImg = ''; if($option['std'] != ''){$prevImg = '<img src='.$option['std'].' alt="" />';} echo '<div class="preview_pic_optionspage" id="'.$option['id'].'_div">'.$prevImg.'</div>'; echo $option['desc'].'<br/>'; echo '<input type="text" size="60" value="'.$option['std'].'" name="'.$option['id'].'" class="upload_pic_input" />'; echo ' <a onclick="return false;" title="" class="cat_up button thickbox" id="'.$option['id'].'" href="media-upload.php?type=image&TB_iframe=true">Insert Image</a>'; } /*******************编辑器****************************/ function tinymce($option){ wp_editor( $option['std'], $option['id'],$settings=array('tinymce'=>1,'media_buttons'=>0,) ); } } ?> $options = array(); $taxonomyinfo = array('category'); $options[] = array( "id" => "_notice", "desc" => '提示信息内容', "type" => "info"); $options[] = array( "name" => "分类关键词", "desc" => "", "id" => "_seo_keywords", "std" => "", "edit_only"=>false, "size" => 40, "type" => "text"); $options[] = array( "name" => "文本域测试", "desc" => "描述信息", "id" => "_textarea", "std" => "", "edit_only"=>false, "size" => 40, "type" => "textarea"); $options[] = array( "name" => "单选框测试", "desc" => "请选择性别", "id" => "_radio", "std" => "sprother", "edit_only"=>false, "buttons" => array( 'man'=>'男性', 'woman'=>'女性', 'sprother'=>'中性' ), "type" => "radio"); $options[] = array( "name" => "复选框--你喜欢哪几个", "id" => "_checkbox", "std" => "", "buttons" => array( 'chun'=>'春哥', 'zeng'=>'曾哥', 'zhu'=>'著姐', 'feng'=>'玉凤', 'fu'=>'芙蓉' ), "edit_only"=>false, "type" => "checkbox"); $options[] = array( "name" => "下拉框框测试-页面", "desc" => "描述信息", "id" => "_drop_page", "std" => "", "edit_only"=>false, "subtype" => 'page', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-分类", "desc" => "描述信息", "id" => "_drop_cat", "std" => "", "edit_only"=>false, "subtype" => 'cat', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-侧边栏", "desc" => "描述信息", "id" => "_drop_sidebar", "std" => "", "edit_only"=>false, "subtype" => 'sidebar', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-自定义", "desc" => "描述信息", "id" => "_drop_custom", "std" => "", "edit_only"=>false, "subtype" => array( 'apple'=>'苹果', 'banana'=>'香蕉', 'orange'=>'桔子', ), "type" => "dropdown"); $options[] = array( "name" => "上传图片测试", "desc" => "", "id" => "_upload", "std" => "", "edit_only"=>true, "type" => "upload" ); $options[] = array( "name" => "tinymce编辑器测试", "desc" => "", "id" => "_tinymce", "std" => "", "edit_only"=>true, "type" => "tinymce" ); $new_taxonomy_feild = new ashu_taxonomy_feild($options, $taxonomyinfo); include_once(get_template_directory() .'/include/class-taxonomy-feild.php'); include_once(get_template_directory() .'/include/simple-term-meta.php'); $_seo_keywords = get_term_meta( $cat->term_id, '_seo_keywords',true );
2、类文件class-taxonomy-feild.php的后面带有类文件的实例化配置方法,按照需求更改即可。
3、获取数据,使用get_term_meta函数,跟get_post_meta函数类似。以上面示例中,分类关键词的获取为例:
<?php /* wordpress分类法自定义字段类文件 Version: 1.0 Author: 树是我的朋友 Author URI: http://www.dba.cn License: /courses/highgrade/313.html */ class ashu_taxonomy_feild{ var $options; var $taxonomyinfo; function ashu_taxonomy_feild($options,$taxonomyinfo){ $this->options = $options; $this->taxonomyinfo = $taxonomyinfo; foreach($this->taxonomyinfo as $taxonomy){ add_action($taxonomy.'_add_form_fields', array(&$this, 'taxonomy_fields_adds'), 10, 2); add_action($taxonomy.'_edit_form_fields', array(&$this, 'taxonomy_metabox_edit'), 10, 2); add_action('created_'.$taxonomy, array(&$this, 'save_taxonomy_metadata'), 10, 1); add_action('edited_'.$taxonomy,array(&$this, 'save_taxonomy_metadata'), 10, 1); add_action('admin_menu', array(&$this, 'init_taxonomy')); add_action('delete_'.$taxonomy, array(&$this,'delete_taxonomy_metadata'),10,1); } } function init_taxonomy(){ if(basename( $_SERVER['PHP_SELF']) == "edit-tags.php") { //注意加载的脚本的url wp_enqueue_style('cate_fields_css', TEMJS_URI. 'cate_fields.css'); wp_enqueue_script('cate_fields_js',TEMJS_URI. 'cate_fields.js'); wp_enqueue_script('thickbox'); wp_enqueue_style('thickbox'); } } /*********添加分类页面*******/ function taxonomy_fields_adds($tag){ foreach($this->options as $option){ if( (!isset($option["edit_only"]) || !$option["edit_only"] ) ){ if (method_exists($this, $option['type'])){ echo '<div class="form-field">'; echo '<label for="'.$option['id'].'" >'.$option['name'].'</label>'; $this->$option['type']($option); echo '</div>'; } } } } /*************编辑分类页面*****************/ function taxonomy_metabox_edit($tag){ foreach($this->options as $option){ if (method_exists($this, $option['type'])){ if(get_term_meta($tag->term_id , $option['id']) !== ""){ $option['std'] = get_term_meta($tag->term_id,$option['id'], true); } echo '<tr class="form-field">'; echo '<th scope="row" valign="top">'; echo '<label for="'.$option['id'].'" >'.$option['name'].'</label>'; echo '</th>'; echo '<td>'; $this->$option['type']($option); echo '</td>'; echo '</tr>'; } } } /**********删除数据******/ function delete_taxonomy_metadata($term_id){ foreach($this->options as $option){ delete_term_meta($term_id,$option['id']); } } /**************保存数据*****************/ function save_taxonomy_metadata($term_id){ foreach($this->options as $option){ if(isset($_POST[$option['id']])){ if(!current_user_can('manage_categories')){ return $term_id ; } if( $option['type'] == 'tinymce' ){ $data = stripslashes($_POST[$option['id']]); }elseif( $option['type'] == 'checkbox' ){ $data = $_POST[$option['id']]; }else{ $data = htmlspecialchars($_POST[$option['id']], ENT_QUOTES,"UTF-8"); } if(get_term_meta($term_id , $option['id']) == "") add_term_meta($term_id , $option['id'], $data, true); elseif($data != get_term_meta($term_id , $option['id'], true)) update_term_meta($term_id , $option['id'], $data); elseif($data == "") delete_term_meta($term_id , $option['id'], get_term_meta($term_id , $option['id'], true)); } } } /******提示信息******/ function info($option){ echo '<p>'.$option['desc'].'</p>'; } /*******文本输入框text*******/ function text($option){ echo '<input type="text" size="'.$option['size'].'" value="'; echo $option['std']; echo '" id="'.$option['id'].'" name="'.$option['id'].'"/>'; echo '<p>'.$option['desc'].'</p>'; } /******文本域***********/ function textarea($option){ echo '<textarea cols="40" rows="5" id="'.$option['id'].'" name="'.$option['id'].'">'.$option['std'].'</textarea>'; echo '<p>'.$option['desc'].'</p>'; } /**********单选框************/ function radio( $option ){ foreach( $option['buttons'] as $key=>$value ) { $checked =""; if( $option['std'] == $key) { $checked = 'checked = "checked"'; } echo '<input '.$checked.' type="radio" class="kcheck" value="'.$key.'" name="'.$option['id'].'"/>'.$value; } } /**************复选框*******************/ function checkbox($option) { foreach( $option['buttons'] as $key=>$value ) { $checked =""; if( is_array($option['std']) && in_array($key,$option['std'])) { $checked = 'checked = "checked"'; } echo '<input '.$checked.' type="checkbox" class="kcheck" value="'.$key.'" name="'.$option['id'].'[]"/>'.$value; } } /***********下拉选择***********/ function dropdown($option){ if($option['subtype'] == 'page') { $select = 'Select page'; $entries = get_pages('title_li=&orderby=name'); }else if($option['subtype'] == 'sidebar'){ global $wp_registered_sidebars; $select = 'Select a special sidebar'; $entries = $wp_registered_sidebars; }else if($option['subtype'] == 'cat'){ $select = 'Select category'; $entries = get_categories('title_li=&orderby=name&hide_empty=0'); }else{ $select = 'Select...'; $entries = $option['subtype']; } echo '<select class="postform" id="'. $option['id'] .'" name="'. $option['id'] .'"> '; echo '<option value="">请选择...</option> '; foreach ($entries as $key => $entry){ if($option['subtype'] == 'page'){ $id = $entry->ID; $title = $entry->post_title; }else if($option['subtype'] == 'cat'){ $id = $entry->term_id; $title = $entry->name; }else if($option['subtype'] == 'sidebar'){ $id = $entry['id']; $title = $entry['name']; }else{ $id = $key; $title = $entry; } $selected=''; if( $option['std'] == $id ){ $selected = "selected='selected'"; } echo"<option $selected value='". $id ."'>". $title."</option>"; } echo '</select>'; echo '<p>'.$option['desc'].'</p>'; } /***************图片上传**********************/ function upload($option){ $prevImg = ''; if($option['std'] != ''){$prevImg = '<img src='.$option['std'].' alt="" />';} echo '<div class="preview_pic_optionspage" id="'.$option['id'].'_div">'.$prevImg.'</div>'; echo $option['desc'].'<br/>'; echo '<input type="text" size="60" value="'.$option['std'].'" name="'.$option['id'].'" class="upload_pic_input" />'; echo ' <a onclick="return false;" title="" class="cat_up button thickbox" id="'.$option['id'].'" href="media-upload.php?type=image&TB_iframe=true">Insert Image</a>'; } /*******************编辑器****************************/ function tinymce($option){ wp_editor( $option['std'], $option['id'],$settings=array('tinymce'=>1,'media_buttons'=>0,) ); } } ?> $options = array(); $taxonomyinfo = array('category'); $options[] = array( "id" => "_notice", "desc" => '提示信息内容', "type" => "info"); $options[] = array( "name" => "分类关键词", "desc" => "", "id" => "_seo_keywords", "std" => "", "edit_only"=>false, "size" => 40, "type" => "text"); $options[] = array( "name" => "文本域测试", "desc" => "描述信息", "id" => "_textarea", "std" => "", "edit_only"=>false, "size" => 40, "type" => "textarea"); $options[] = array( "name" => "单选框测试", "desc" => "请选择性别", "id" => "_radio", "std" => "sprother", "edit_only"=>false, "buttons" => array( 'man'=>'男性', 'woman'=>'女性', 'sprother'=>'中性' ), "type" => "radio"); $options[] = array( "name" => "复选框--你喜欢哪几个", "id" => "_checkbox", "std" => "", "buttons" => array( 'chun'=>'春哥', 'zeng'=>'曾哥', 'zhu'=>'著姐', 'feng'=>'玉凤', 'fu'=>'芙蓉' ), "edit_only"=>false, "type" => "checkbox"); $options[] = array( "name" => "下拉框框测试-页面", "desc" => "描述信息", "id" => "_drop_page", "std" => "", "edit_only"=>false, "subtype" => 'page', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-分类", "desc" => "描述信息", "id" => "_drop_cat", "std" => "", "edit_only"=>false, "subtype" => 'cat', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-侧边栏", "desc" => "描述信息", "id" => "_drop_sidebar", "std" => "", "edit_only"=>false, "subtype" => 'sidebar', "type" => "dropdown"); $options[] = array( "name" => "下拉框框测试-自定义", "desc" => "描述信息", "id" => "_drop_custom", "std" => "", "edit_only"=>false, "subtype" => array( 'apple'=>'苹果', 'banana'=>'香蕉', 'orange'=>'桔子', ), "type" => "dropdown"); $options[] = array( "name" => "上传图片测试", "desc" => "", "id" => "_upload", "std" => "", "edit_only"=>true, "type" => "upload" ); $options[] = array( "name" => "tinymce编辑器测试", "desc" => "", "id" => "_tinymce", "std" => "", "edit_only"=>true, "type" => "tinymce" ); $new_taxonomy_feild = new ashu_taxonomy_feild($options, $taxonomyinfo); include_once(get_template_directory() .'/include/class-taxonomy-feild.php'); include_once(get_template_directory() .'/include/simple-term-meta.php'); $_seo_keywords = get_term_meta( $cat->term_id, '_seo_keywords',true );
FAQ:
1、能正确显示表单,但是无法保存数据?
本站提供的simple-term-meta.php文件在安装激活主题的时候,会在数据库中添加一个_termmeta表用来保存数据,如果无法保存数据,八成是未新建该数据表,请重新激活主题。
2、下载的压缩包是空的?
请换浏览器、换电脑。
3、如何使用该类文件给自定义分类法添加字段?
在配置代码中$taxonomyinfo数组,加入你自己的自定义分类法。