nesting level 2 categories in dropdown
authorAnthony Talarico <talarico@gaslightmedia.com>
Mon, 21 Dec 2015 17:50:02 +0000 (12:50 -0500)
committerAnthony Talarico <talarico@gaslightmedia.com>
Mon, 21 Dec 2015 17:50:02 +0000 (12:50 -0500)
index.php

index bd100d9..b5f014e 100644 (file)
--- a/index.php
+++ b/index.php
@@ -173,11 +173,12 @@ function michsci_shortcode($atts)
 
     echo '<form id="catSubmit" action=""><div class="row catSearch"><div id="viewBtns" class="12-small medium-3 columns"> <button id="grid" type="button">Grid</button> <button id="list"type="button">list</button> 
                 </div><div id="catDropdown" class="small-12 medium-6 columns">';
-
     echo michsci_get_terms_dropdown();
     echo '</div><div id="catSearchBtn" class="small-12 medium-3 columns"><input type="submit" value="search" class="button"></div></div>';
     echo '</form>';
     
+  
+    
     if ( $product_id = filter_var( $_REQUEST['product'], FILTER_VALIDATE_INT ) ) {
         michsci_show_product($product_id);
     } else if ( $cat_id = filter_var( $_REQUEST['category'], FILTER_VALIDATE_INT ) ) {
@@ -194,8 +195,6 @@ function michsci_shortcode($atts)
 
 function michsci_get_terms_dropdown()
 {
-    
-    
     $taxonomyName = "michsci_category";
     $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'name', 'hide_empty' => false)); 
     $output ="<select id='categoryDropdown' name=\"michsci_category\"><option value=\"\">Select Category</option>";
@@ -207,22 +206,30 @@ function michsci_get_terms_dropdown()
             $child_terms = get_terms($taxonomyName, array('child_of' => $parent->term_id, 'orderby' => 'name', 'hide_empty' => false));
             $output .= '<option value="' . $parent->term_id . '"';
             if ($_REQUEST['michsci_category'] == $parent->term_id) {
-            $output .= ' selected';
-         }
-         $output .= '>' . $parent->name . '</option>';
+                $output .= ' selected';
+            }
+            $output .= '>' . $parent->name . '</option>';
             foreach($child_terms as $child){
+                $grandchildren = get_terms($taxonomyName, array('child_of' => $child->term_id, 'orderby' => 'name', 'hide_empty' => false));
                  $output .= '<option value="' . $child->term_id . '"';
                 if ($_REQUEST['michsci_category'] == $child->term_id) {
-             $output .= ' selected';
-        }
-        $output .= '>' . "&nbsp&nbsp&nbsp" .  $child->name . '</option>';
-            
+                    $output .= ' selected';
+                }
+                $output .= '>' . "&nbsp&nbsp&nbsp" .  $child->name . '</option>';
+                foreach($grandchildren as $grandchild){
+                    $output .= '<option value="' . $grandchild->term_id . '"';
+                    if ($_REQUEST['michsci_category'] == $grandchild->term_id) {
+                        $output .= ' selected';
+                    }
+                    $output .= '>' . "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" .  $grandchild->name . '</option>';
+                }
             }
         } 
     $output .="</select>";
     return $output;
 }
 
+
 function michsci_list_products()
 {
     global $wpdb, $wp;