Tagged: ,

This topic contains 5 replies, has 3 voices, and was last updated by  Steve 2 years, 9 months ago.

  • Author
    Posts
  • #5882

    helmiaditya14
    Participant

    I found this post and it’s exactly what I need from a custom taxonomy select. However, I’m not too savvy with the codes to dynamically generate it properly from a custom taxonomy. Anybody could help?

  • #5894

    Steve
    Keymaster

    @helmiaditya14– Happy to help. I’m just not clear on the request. What do you need help with, creating the actual taxonomy?

  • #5906

    helmiaditya14
    Participant

    I was trying to replicate this code into the front end form.

    piklist('field', array(
        'type' => 'select'
        ,'field' => 'select_optgroup'
        ,'label' => __('Select with Option Groups', 'piklist-demo')
        ,'value' => 'third'
        ,'choices' => array(
          'Group 1' => array(
            'first' => __('First Choice', 'piklist-demo')
            ,'second' => __('Second Choice', 'piklist-demo')
            ,'third' => __('Third Choice', 'piklist-demo')
          )
          ,'Group 2' => array(
            'first' => __('First Choice', 'piklist-demo')
            ,'second' => __('Second Choice', 'piklist-demo')
            ,'third' => __('Third Choice', 'piklist-demo')
          )
        )
      ));

    But I can’t figure how to adjust the example to populate custom taxonomy. Any help appreciated!

  • #5928

    helmiaditya14
    Participant

    I didn’t know PHP that much to alter the code to generate custom taxonomy select dropdown with parent terms as optgroup. Anyone could help me?

    Thanks!

  • #7540

    buishi
    Participant

    I know this is old, but this is how I did it:

    $parents = get_terms(array(
      'taxonomy' => 'case_category'
      ,'hide_empty' => false
      ,'parent' => 0
      ));
    
      function choices($parent_cats){
        $cats = [];
        foreach ($parent_cats as $parent) {
          $cats[$parent->name] = piklist(
          get_terms('case_category', array(
            'hide_empty' => false
            ,'child_of' =>$parent->term_id
          )), array(
            'term_id','name'
          ));
        }
        return $cats;
      }
      // var_dump(choices($parents));
    
        piklist('field', array(
          'type' => 'select',
          'scope' => 'taxonomy',
          'field' => 'case_category',
          'label' => 'Category',
          'attributes' => array(
            'class' => 'form-control'
          ),
          'choices' => choices($parents),
        'required' => true,
        ));
    
  • #7560

    Steve
    Keymaster

    @buishi– That looks good to me!

The topic ‘Custom Taxonomy with Optgroup’ is closed to new replies.