最简单的方法是注销分类法的元盒,并用您自己的自定义元盒替换它。下面是我的尝试。虽然有一些缺点,但如果没有添加一些javascript,我无法复制WordPress的“添加新术语”功能,因此您只能选择预先存在的类别。(至少我是这样,但它添加了一个复选框而不是单选按钮)。
class My_Radio_Tax{
static $taxonomy = \'event-category\'; //Slug of taxonomy
static $post_type = \'event\';//Post type for meta-box
function load(){
add_action( \'admin_menu\', array(__CLASS__,\'remove_meta_box\'));
add_action( \'add_meta_boxes\', array(__CLASS__,\'add_meta_box\'));
}
//Remove taxonomy meta box
function remove_meta_box(){
//The taxonomy metabox ID. This is different for non-hierarchical taxonomies
$tax_mb_id = self::$taxonomy.\'div\';
remove_meta_box($tax_mb_id, self::$post_type, \'normal\');
}
//Add new taxonomy meta box
function add_meta_box() {
add_meta_box( \'my_tax\', \'My taxonomy\',array(__CLASS__,\'metabox_inner\'),\'event\' ,\'side\',\'core\');
}
//Callback to set up metabox
function metabox_inner( $post ) {
//Get taxonomy and terms
$taxonomy = self::$taxonomy;
$tax = get_taxonomy($taxonomy);
$name = \'tax_input[\' . $taxonomy . \']\';
$terms = get_terms(\'event-category\',array(\'hide_empty\' => 0));
//Get current and popular terms
$popular = get_terms( $taxonomy, array( \'orderby\' => \'count\', \'order\' => \'DESC\', \'number\' => 10, \'hierarchical\' => false ) );
$postterms = get_the_terms( $post->ID,$taxonomy );
$current = ($postterms ? array_pop($postterms) : false);
$current = ($current ? $current->term_id : 0);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
<!-- Display tabs-->
<ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
<li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php echo $tax->labels->all_items; ?></a></li>
<li class="hide-if-no-js"><a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e( \'Most Used\' ); ?></a></li>
</ul>
<!-- Display popular taxonomy terms -->
<div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;">
<ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
<?php foreach($popular as $term){
$id = "id=\'in-popular-event-category-$term->term_id\'";
echo "<li id=\'popular-event-category-$taxonomy-$term->term_id\'><label class=\'selectit\'>";
echo "<input type=\'radio\' {$id}".checked($current,$term->term_id,false)."value=\'$term->term_id\' />$term->name<br />";
echo "</label></li>";
}?>
</ul>
</div>
<!-- Display taxonomy terms -->
<div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">
<ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
<?php foreach($terms as $term){
$id = "id=\'in-event-category-$term->term_id\'";
echo "<li id=\'event-category-$taxonomy-$term->term_id\'><label class=\'selectit\'>";
echo "<input type=\'radio\' {$id} name=\'{name}\'".checked($current,$term->term_id,false)."value=\'$term->term_id\' />$term->name<br />";
echo "</label></li>";
}?>
</ul>
</div>
</div>
<?php
}
}
My_Radio_Tax::load();