通过情况下国内的wordpress主题很少会应用到自定义分类及自定义分数的字段拓展,或是做为一款综合主题,对于自定义分类的添加是非常必要的,今天大挖帮助大家拓展一下知识而,来了解一下如何运用如下代码简单的完成为自定义分类添加字段拓展的功能,
为wordpress自定义分类添加字段拓展原理也很简单,和给默认文章添加字段的功能区别不大,我们拿为自定义分类添加颜色字体为例。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
function ruike_add_category_field(){ echo '<div class="form-field"> <label for="cat-color">颜色</label> <input name="cat-color" id="cat-color" type="text" value="" size="40"> <p>在文章列表的标题前显示</p> </div>'; } add_action('category_add_form_fields','ruike_add_category_field'); add_action('category_edit_form_fields','ruike_add_category_field'); function ruike_add_buluo_metadate($cat_term_id){ if(isset($_POST['buluo-tax-ico'])){ if(!current_user_can('manage_categories')){ return $cat_term_id; } //你的逻辑代码 } add_action('created_buluo_category','ruike_add_buluo_metadate'); add_action('edited_buluo_category','ruike_add_buluo_metadate'); //部落新增编辑分类自增字段1.7 function ruike_add_buluo_field($term){ $buluo_tax_value = get_term_meta( $term->term_id, 'buluo_tax_ico', true ); echo '<tr class="form-field"> <th scope="row" valign="top"><label for="cat_Image_url">图标URL</label> <td><input name="buluo_tax_ico" id="buluo_tax_ico" type="text" value="'.esc_attr( $buluo_tax_value ).'" size="40"><br /> <span class="description">在部落社区列表头部显示</span> </td> </tr>'; } add_action('buluo_category_edit_form_fields','ruike_add_buluo_field',10,2); add_action('buluo_category_add_form_fields','ruike_add_buluo_field',10,2); function ruike_add_buluo_metadate($term_id){ if(!current_user_can('manage_categories')){ return $term_id; } $buluo_tax_value = $_POST['buluo_tax_ico']; if ( '' === $buluo_tax_value ) { delete_term_meta( $term_id, 'buluo_tax_ico' ); } else { update_term_meta( $term_id, 'buluo_tax_ico', $buluo_tax_value ); } } add_action('created_buluo_category','ruike_add_buluo_metadate',10,1); add_action('edited_buluo_category','ruike_add_buluo_metadate',10,1); //部落信息列图标 function ruike_edit_term_columns( $columns ) { $columns['buluo_ico'] = '图标'; return $columns; } add_filter( 'manage_edit-buluo_category_columns', 'ruike_edit_term_columns' ); function ruike_manage_term_custom_column( $out, $column, $term_id ) { if ( 'buluo_ico' == $column ) { $buluo_tax_value = get_term_meta( $term_id, 'buluo_tax_ico', true ); if ( ! $buluo_tax_value ) $buluo_tax_value = THEME_URI.'/img/tax-ico.jpg'; $out = '<img src="'.$buluo_tax_value.'" width="35" height="35" style="border-radius: 50%;">'; } return $out; } add_filter( 'manage_buluo_category_custom_column', 'ruike_manage_term_custom_column', 10, 3 ); |