• Dan

    (@danscott1121)


    I have added a custom taxonomy to Users – user categories.

    Using the code below, I am able to output the custom taxonomies in each edit-user profile page:

    function show_user_category( $user ) {
    
    //get the terms that the user is assigned to
    $assigned_terms = wp_get_object_terms( $user->ID, 'user_category' );
    $assigned_term_ids = array();
    foreach( $assigned_terms as $term ) {
        $assigned_term_ids[] = $term->term_id;
    }
    
    //get all the terms we have
    $user_cats = get_terms( 'user_category', array('hide_empty'=>false) );
    
    echo "<h3>User Category</h3>";
    
    //list the terms as checkbox, make sure the assigned terms are checked
    foreach( $user_cats as $cat ) { ?>
        <input type="checkbox" id="user-category-<?php echo $cat->term_id ?>" <?php if(in_array( $cat->term_id, $assigned_term_ids )) echo 'checked=checked';?> name="user_category[]"  value="<?php echo $cat->term_id;?>"/>
        <?php
        echo '<label for="user-category-'.$cat->term_id.'">'.$cat->name.'</label>';
        echo '<br />';
    }
    }

    The code above simply displays a list of checkboxes, ordered by term_id.

    Of course, I want to display them in the same way that custom taxonomy terms would be displayed in a custom post type (a scrollable list of checkboxes, with child terms indented and underneath their parent term). The code above does not display the terms in order of parents/children.

    Is there a WP function I can pass my taxonomy & terms to, to create what I described in the paragraph above? Or do I have to do it manually?

    Thanks

  • The topic ‘WP function to display custom taxonomies in WP-Admin Style?’ is closed to new replies.