Skip to content

Instantly share code, notes, and snippets.

@5ally
Created March 18, 2021 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 5ally/f087701e3791902cfe2324daadfa86d3 to your computer and use it in GitHub Desktop.
Save 5ally/f087701e3791902cfe2324daadfa86d3 to your computer and use it in GitHub Desktop.
<?php
// Add this to your functions file.
function the_wpse_384435_checklist( $taxonomy, $label = null ) {
// Make sure the taxonomy exists.
if ( ! $tax = get_taxonomy( $taxonomy ) ) {
return false;
}
$tax_input = isset( $_GET['tax_input'] ) ?
(array) $_GET['tax_input'] : array();
$selected_cats = ( ! empty( $tax_input[ $taxonomy ] ) ) ?
wp_parse_id_list( $tax_input[ $taxonomy ] ) : array();
// Just modify the HTML to your very own liking. :)
echo '<div>';
$label = $label ? $label : $tax->labels->singular_name;
echo '<label>Filter by ' . esc_html( $label ) . ':</label>';
wpse_384435_checklist( array(
'taxonomy' => $taxonomy,
'selected_cats' => $selected_cats,
'checked_ontop' => false,
) );
echo '</div>';
}
/* Usage examples:
In your template files, display the checklist like so:
the_wpse_384435_checklist( 'salary_level', 'Salary' );
the_wpse_384435_checklist( 'jobs_categories', 'Job Type' );
You may also omit the second parameter and the function will use the default
label set when the taxonomy is registered:
the_wpse_384435_checklist( 'salary_level' );
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment