如何将自定义分类更改为单选按钮

时间:2012-03-01 作者:John

实际上,我在这里找到了解决问题的方法:Altering the appearance of custom taxonomy inputs 但我想问一个后续问题,但似乎我无法在这条线索中提问。

下面是该线程上提供的代码,它似乎可以工作。

add_action(\'add_meta_boxes\',\'mysite_add_meta_boxes\',10,2);
    function mysite_add_meta_boxes($post_type, $post) {
    ob_start();
}
add_action(\'dbx_post_sidebar\',\'mysite_dbx_post_sidebar\');

function mysite_dbx_post_sidebar() {
    $html = ob_get_clean();
    $html = str_replace(\'"checkbox"\',\'"radio"\',$html);
    echo $html;
}
当我使用上面的代码时,它会将所有分类框中的复选框转换为单选按钮。但我只想要3个复选框将其复选框更改为单选按钮。那么,如何将复选框转换为特定分类框的单选按钮呢?

tnx。

2 个回复
SO网友:helgatheviking

希望它不会被认为过于自我推销,但我已经将stephen harris的代码转换为一个插件,可以为您实现这一点:http://wordpress.org/extend/plugins/radio-buttons-for-taxonomies/

我还在努力让快速编辑显示单选按钮,但这似乎要困难得多。

SO网友:Stephen Harris

最简单的方法是注销分类法的元盒,并用您自己的自定义元盒替换它。下面是我的尝试。虽然有一些缺点,但如果没有添加一些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();

结束

相关推荐