added leveling for child categories
authorAnthony Talarico <talarico@gaslightmedia.com>
Fri, 18 Dec 2015 15:02:01 +0000 (10:02 -0500)
committerAnthony Talarico <talarico@gaslightmedia.com>
Fri, 18 Dec 2015 15:02:01 +0000 (10:02 -0500)
index.php

index 55bbc1a..ad5688f 100644 (file)
--- a/index.php
+++ b/index.php
@@ -173,6 +173,7 @@ function michsci_shortcode($atts)
 
     echo '<form action=""><div class="row"><div class="small-12 medium-9 columns">';
     echo michsci_get_terms_dropdown();
+  
     echo '</div><div class="small-12 medium-3 columns"><input type="submit" value="search" class="button"></div></div>';
     echo '</form>';
     
@@ -192,22 +193,47 @@ function michsci_shortcode($atts)
 
 function michsci_get_terms_dropdown()
 {
-    $myterms = get_terms('michsci_category', array(
-            'orderby'    => 'name',
-            'hide_empty' => true
-        ));
+    $taxonomyName = "michsci_category";
+
+    $parent_terms = get_terms($taxonomyName, array('parent' => 0, 'orderby' => 'name', 'hide_empty' => false)); 
+//
+//    
+//    $myterms = get_terms('michsci_category', array(
+//            'orderby'    => 'id',
+//            'hierarchical' => 1,
+//            'hide_empty' => false
+//        ));
     $output ="<select name=\"michsci_category\"><option value=\"\">Select Category</option>";
-    foreach ( $myterms as $term ) {
-        $term_taxonomy = $term->taxonomy;
-        $term_slug = $term->slug;
-        $term_name = $term->name;
-        $link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;
-        $output .= '<option value="' . $term->term_id . '"';
-        if ($_REQUEST['michsci_category'] == $term->term_id) {
+  
+//        $term_name = $term->name;
+//        
+//        $term_taxonomy = $term->taxonomy;
+//        $term_slug = $term->slug;
+//        $term_name = $term->name;
+//        $link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;
+       
+        // get parent categories
+        foreach ($parent_terms as $parent){
+            $term_name = $parent->name;
+            $term_taxonomy = $parent->taxonomy;
+            $term_slug = $parent->slug;
+            $term_name = $parent>name;
+            // get children of each category
+            $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>';
+            foreach($child_terms as $child){
+                 $output .= '<option value="' . $child->term_id . '"';
+                if ($_REQUEST['michsci_category'] == $child->term_id) {
              $output .= ' selected';
         }
-        $output .= '>' . $term_name . '</option>';
-    }
+        $output .= '>' . "&nbsp&nbsp&nbsp" .  $child->name . '</option>';
+            
+            }
+        } 
     $output .="</select>";
     return $output;
 }