WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display tag admin box like categories, without hierarchy. (6 posts)

  1. TCBarrett
    Member
    Posted 2 years ago #

    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?

  2. John Blackbourn
    Member
    Posted 2 years ago #

    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

  3. leonmagee
    Member
    Posted 1 year ago #

    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.

  4. TCBarrett
    Member
    Posted 1 year ago #

    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/

  5. leonmagee
    Member
    Posted 1 year ago #

    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
        }
  6. leonmagee
    Member
    Posted 1 year ago #

    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');
     }

Topic Closed

This topic has been closed to new replies.

About this Topic