Marcos_Alexandre

Dropdown Custom Taxonomy Terms

Sep 18th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. <?php
  2. $categories = get_categories( array(
  3.     'orderby' => 'name',
  4.     'taxonomy' => 'italy',
  5.     'parent'  => 0
  6. ) );
  7.  
  8. $url = site_url().'/italy/'; //Slug da custom taxonomy
  9. $select = "<select name='cat' id='cat' class='postform'>n"; //Definir o ID que será utilizado no JS
  10. $select.= "<option value='-1'>Select City</option>n"; //Primeiro item do select
  11.  
  12. foreach($categories as $category){
  13.     if($category->count > 0){
  14.         $select.= "<option value='".$url.$category->slug."'>".$category->name."</option>";
  15.     }
  16. }
  17.  
  18. $select.= "</select>";
  19.  
  20. echo $select;
  21. ?>
  22.  
  23. <script type="text/javascript"><!--
  24.     var dropdown = document.getElementById("cat");
  25.     function onCatChange() {
  26.         if ( dropdown.options[dropdown.selectedIndex].value != -1 ) {
  27.             location.href = dropdown.options[dropdown.selectedIndex].value+"/";
  28.         }
  29.     }
  30.     dropdown.onchange = onCatChange;
  31. </script>
Advertisement
Add Comment
Please, Sign In to add comment