ajaydsouza
1/7/2015 - 7:38 PM

Taxonomy checkbox list

Taxonomy checkbox list

/**
 * returns taxonomy lists that have children with checkboxes
 * 
 */
  function get_subject_list($taxonomy){
    // Sets the taxonomy to pull a list of terms from.
    // Throws them into a nifty object for referencing and counting
    $terms = get_terms($taxonomy,array('parent' => 0));

    if ($terms) {
        // For each term we will create some html for use in styling
        // and additional hooks for functionality
      foreach($terms as $term) {
        // We can return a variety of things to enhance functionality
        // Here we are returning the slug for use as a class (useful for setting up module specific sprites n etc)
        echo '<div class="check-bx ' . $term->slug .'">';
        // Here we are returning the term name to be used as a unique identifier for this set of checkboxes
        echo '<input type="checkbox" class="css-checkbox" name="' . $term->slug .'" id="' . $term->slug .'">';
        // Here we extend that by also passing the number of items that are within the section that are published.        
        echo '<label class="css-label lite-red-check" data-tax-primary="' . $taxonomy . '" for="' . $term->slug .'" data-tax-name="' . $term->name .'" data-tax-slug="' . $term->slug . '"> '. $term->name .' <span class="available-items">('. $term->count . ')</span>';
        // echo '</div>';   
        // We set the variable for any child terms   
        echo '</div>';
        $term_children = get_term_children($term->term_id,$taxonomy);
        if ( count( get_term_children( $term->term_id, $taxonomy ) ) > 0 ) {
            // The term has children display nothing
            echo '<div class="collapsible collapsed">';
            echo '<h3>' . $term->name .'</h3>';
            foreach($term_children as $term_child_id) {
                // We'll list out each term and also display any child taxonomy terms
                $term_child = get_term_by('id',$term_child_id,$taxonomy);
                echo '<div class="check-bx blue">';
                // echo '<li><a href="' . get_term_link( $term_child->term_id, $taxonomy ) . '">' . $term_child->name . '</a></li>';
                echo '<input type="checkbox" class="css-checkbox" name="' . $term_child->slug .'" id="' . $term_child->slug . '">';
                echo '<label class="css-label lite-red-check" data-tax-primary="'. $taxonomy .'" for="' . $term_child->slug .'" data-tax-name="' . $term_child->name . '" data-tax-slug="' . $term_child->slug . '"> '. $term_child->name .' ';
                echo '</div>';
            }
            echo '</div>';
        }
      if (  count( get_term_children( $term->term_id, $taxonomy ) ) === 0 ) {
        // The term has no children display nothing
      }        

         
      }
    }   
 }