1

I'm trying to get anoter level subcategory to show in my select option.

I've managed to get one... how can i get the other?

<?php
    $taxonomies = array(CUSTOM_CAT_TYPE);
    $args = array('orderby' => 'count', 'hide_empty' => 0);

    $myterms = get_terms($taxonomies, $args);
    $output = "<select class=\"select\" name='select_cat' title=\"Select Category\">";
    $output .= "<option value=''>Selecteer Rubriek</option>";


    foreach ($myterms as $cate)
        {                       
        if($cate->parent == 0)
            {
            $catename = "";
            $catename = $cate->name;
            $output .='<option disabled="disabled" value="'.$cate->term_id.'">'.$catename. '</option>';
                foreach ($myterms as $subcate)
                    {
                    if($subcate->parent == $cate->term_id)
                        {                                   
                        $catename = "";
                        $catename = $subcate->name;
                        $output .='<option value="'.$subcate->term_id.'">'.'&nbsp;&nbsp;&nbsp;&nbsp;'.$catename.'</option>';
                            foreach ($subcate as $subsubcate)
                                {
                                if($subsubcate->parent == $cate->term_id)
                                    {                                   
                                    $catename = "";
                                    $catename = $subsubcate->name;
                                    $output .='<option value="'.$subsubcate->term_id.'">'.'&nbsp;&nbsp;&nbsp;&nbsp;'.$catename.'</option>';

                                    }
                                }
                        }
                    }
                }
        }


    $output .="</select>";
    echo $output;

    ?>

Any ideas?! Or perhaps a better way to get where I want to be! :)

  • Your code generates some Notices but seems to work. What exactly is the problem and how exactly is it WordPress specific? – s_ha_dum Nov 19 '13 at 5:44
  • Well it works for 1 level subcategory, but i need to get another leven of subcategories. cat - sub cat -- sub sub cat I'm using it in a advanced search i'm trying to build for my wordpress theme and uses a custom category (ads) – 2611 Nov 19 '13 at 8:43
  • Ok. I get it. Try var_dump($myterms);. You will notice that the output is one dimensional. You will need to convert that one dimensional array into a multidimensional (nested) one before you can foreach over it like you are trying to do. That is a PHP question though. Search "convert flat array to nested". – s_ha_dum Nov 19 '13 at 14:56
  • I've done the var_dump. You can see the results here: takchitaplanet.nl/test-page. I'm kinda new to PHP and looked into the flat array to nested... But whatever i try, I won't get a nice tree structure. Where to go from here? – 2611 Nov 20 '13 at 19:29
1

Found the solution! :)

$taxonomies = array(CUSTOM_CAT_TYPE);
        foreach($taxonomies as $tax)
         {
             $args = array(
                  'orderby'         => 'name',
                  'show_count'      => 1,
                  'pad_counts'      => 1,
                  'hierarchical'    => 1,
                  'hide_empty'      => 0,
                  'taxonomy'        => $tax,
                );

             wp_dropdown_categories( $args );
         }

Thanks for helping out!

  • 1
    Please accept your answer so the question is no longer considered unanswered; click the check mark under the vote arrows on the left. – Gabriel Jul 5 '16 at 16:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.