有时在WordPress上开发某些内容时,必须使用字段 术语组 从数据库表 wp_term_relationships。 例如,按此字段对分类术语进行排序。 但是如何分配 术语组 默认情况下没有这样的界面时,每个术语的值是多少? 下面介绍的PHP类将帮助您解决此任务:
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 | class TaxonomyTermGroup { public function init($taxonomies) { if (!empty($taxonomies)) { foreach ($taxonomies as $taxonomy_name) { add_filter('manage_edit-' . $taxonomy_name . '_columns', array(__CLASS__, 'draw_taxonomy_columns')); add_filter('manage_' . $taxonomy_name . '_custom_column', array(__CLASS__, 'draw_taxonomy_columns_data'), 5, 3); add_action($taxonomy_name . "_edit_form_fields", array(__CLASS__, "edit_form_fields"), 10, 2); } } } public static function draw_taxonomy_columns($columns) { return array_merge($columns, array('term_group' => __('Term group', ''))); } public static function draw_taxonomy_columns_data($value = '', $column_name = '', $term_id = 0) { switch ($column_name) { case 'term_group': $term = get_term_by('term_id', $term_id, @$_GET['taxonomy']); if (is_object($term)) { echo $term->term_group; } else { echo 0; } break; } } public static function edit_form_fields($term, $taxonomy) { ?> <tr class="form-field form-required"> <th scope="row" valign="top"><label for="term_group"><?php _e('Term group', '') ?></label></th> <td> <select name="term_group" id="term_group"> <?php for ($i = 0; $i < 8; $i++): ?> <option value="<?php echo $i ?>" <?php echo($i == $term->term_group ? "selected" : "") ?>><?php echo $i ?></option> <?php endfor; ?> </select> </td> </tr> <?php } } |
使用此类很简单。 在初始化分类法代码之后,您应该添加下一段代码:
1 2 | $taxt=new TaxonomyTermGroup(); $taxt->init(array('clients','skills')); |
客户 和 技能 这是分类法的标语。