Skip to content

Instantly share code, notes, and snippets.

@dsmy
Last active June 26, 2019 10:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsmy/0215640bf15512ce0b75 to your computer and use it in GitHub Desktop.
Save dsmy/0215640bf15512ce0b75 to your computer and use it in GitHub Desktop.
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
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment