• Resolved TCBarrett

    (@tcbarrett)


    I want to register a custom taxonomy, without hierarchy, but have the back-end work like categories do and not like tags.

    That is I want the (tags) to be listed with checkboxes by them (like categories are), but I don’t want hierarchical children available.

    Is there a nice way to do this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    It’s easy enough. You just need to remove the default meta box and then add your own one which uses wp_terms_checklist() to output the terms.

    I’ve pasted a code example here: http://pastebin.com/MswB57Fn

    This works sort of if you make the action hook ‘add_meta_boxes’, but when you select a term and save, it adds a number to the list of taxonomy terms instead of saving properly.

    Thread Starter TCBarrett

    (@tcbarrett)

    Tags and Categories are handled differently (name vs id AFAIR).

    I blogged an example based on this here:
    http://www.tcbarrett.com/2012/05/turn-your-taxonomy-into-radio-select-using-wordpress-walker-function/

    Thanks TCBarrett. I found a similar article here: http://wp.tutsplus.com/tutorials/creative-coding/how-to-use-radio-buttons-with-taxonomies/ There was some good info in the comments that helped me out.

    Here is my final code, to make a non-hierarchical custom taxonomy named ‘mens_rings’ display in the admin like categories would, for the custom post type ‘product’:

    add_action( 'add_meta_boxes', 'gpj_change_meta_box');
        function gpj_change_meta_box() {
        remove_meta_box('tagsdiv-mens_rings', 'product', 'normal');
        add_meta_box( 'gpj-mens_rings', 'Men\'s Rings','gpj_mytaxonomy_metabox','product' ,'side','core');
    }  
    
        function gpj_mytaxonomy_metabox($post) {  
    
            $taxonomy = 'mens_rings';  
    
            // all terms of ctax
            $all_ctax_terms = get_terms($taxonomy,array('hide_empty' => 0)); 
    
            // all the terms currenly assigned to the post
            $all_post_terms = get_the_terms( $post->ID,$taxonomy );  
    
            // name for each input, notice the extra []
            $name = 'tax_input[' . $taxonomy . '][]';  
    
            // make an array of the ids of all terms attached to the post
            $array_post_term_ids = array();
            if ($all_post_terms) {
                foreach ($all_post_terms as $post_term) {
                    $post_term_id = $post_term->term_id;
                    $array_post_term_ids[] = $post_term_id;
                }
            }
    
            ?>
    
    <div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv"> 
    
            <input type="hidden" name="<?php echo $name; ?>" value="0" />
    
            <ul>
                <?php   foreach($all_ctax_terms as $term){
                        if (in_array($term->term_id, $array_post_term_ids)) {
                            $checked = "checked = ''";
                        }
                        else {
                            $checked = "";
                        }
                    $id = $taxonomy.'-'.$term->term_id;
                    echo "<li id='$id'>";
                    echo "<input type='checkbox' name='{$name}'id='in-$id'"
                    . $checked ."value='$term->slug' /><label> $term->name</label><br />";
                   echo "</li>";
                }?>
           </ul>
    </div>
    <?php
        }

    Instead of the first ‘add action’ it should be this:

    // remove old meta box
    add_action( 'admin_menu', 'gpj_remove_meta_box');
    function gpj_remove_meta_box() {
       remove_meta_box('tagsdiv-mens_rings', 'product', 'normal');
    } 
    
    //Add new taxonomy meta box
     add_action( 'add_meta_boxes', 'gpj_add_meta_box');
     function gpj_add_meta_box() {
         add_meta_box( 'gpj-mens_rings', 'Mens Rings','gpj_mytaxonomy_metabox','product' ,'side','core');
     }
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display tag admin box like categories, without hierarchy.’ is closed to new replies.