Last active
June 14, 2019 10:44
-
-
Save developer-anuragsingh/1ba0311a842d51f9d14fa1e31ea2929b to your computer and use it in GitHub Desktop.
Add a dropdown filter for wordpress taxonomies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function get_terms_dropdown_type($taxonomy, $args) | |
| { | |
| $taxo_obj = get_taxonomy($taxonomy); | |
| $taxo_singular_name = $taxo_obj->labels->singular_name; | |
| $terms_obj = get_terms($taxonomy, $args); | |
| if ('category' === $taxonomy) { | |
| $taxonomy_name = 'cat'; | |
| } elseif ('post_tag' === $taxonomy) { | |
| $taxonomy_name = 'tag'; | |
| } else { | |
| $taxonomy_name = $taxonomy; | |
| } | |
| $output = "<select name='" . $taxonomy_name . "'>"; //CHANGE ME! | |
| $output .= "<option value=''>Select " . $taxo_singular_name . "</option>"; //CHANGE ME TO YOUR LIKING! | |
| foreach ($terms_obj as $term) { | |
| // $root_url = get_bloginfo('url'); | |
| // $term_taxonomy = $term->taxonomy; | |
| $term_slug = $term->slug; | |
| $term_name = $term->name; | |
| $link = $term->term_id; | |
| $output .= "<option value='" . $term_slug . "'>" . $term_name . "</option>"; | |
| } | |
| $output .= "</select>"; | |
| return $output; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <form action="<?php bloginfo('url'); ?>" method="get"> | |
| <div> | |
| <?php | |
| $taxonomies = array('TAXO_NAME#1', 'TAXO_NAME#2', 'TAXO_NAME#3'); //CHANGE ME! | |
| $args = array('orderby' => 'name', 'hide_empty' => true); | |
| foreach ($taxonomies as $taxonomy) { | |
| $select = get_terms_dropdown_type($taxonomy, $args); | |
| $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select); | |
| echo $select; | |
| } | |
| ?> | |
| <input type="submit" name="submit" value="filter" /> | |
| </div> | |
| </form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment