0

I need wordpress category as drop down . So far my code

function cat_drop_down(){
  $categories_array = array();
  $categories = get_categories();
    foreach( $categories as $category ){
      $categories_array[] = $category->term_id;
    }
  return $categories_array;
}

This renders drop down perfectly but its not passing the category id . Its rendering html like this

<select data-setting="tab_title">               
   <option value="0">2</option>             
   <option value="1">14</option>                
   <option value="2">1</option>             
</select>

So I am only getting 0,1,2 values instead of 2,14,1 that category id's. What am I doing wrong?

0

4 Answers 4

2

You are partially correct , you are not passing cat id in your parameter. Try

$categories_array[ $category->term_id ] = $category->name;

Now you will get category id as drop down value.

1
1
      function cat_drop_down(){
      $categories_array = array();
      $args = array(
        'type'                     => 'post',
        'child_of'                 => 0,
        'parent'                   => '',
        'orderby'                  => 'name',
        'order'                    => 'ASC',
        'hide_empty'               => false,
        'hierarchical'             => 1,
        'exclude'                  => '',
        'include'                  => '',
        'number'                   => '',
        'taxonomy'                 => 'category',
        'pad_counts'               => false
    );

      $categories = get_categories($args);
        $i =0;
        foreach( $categories as $category ){
          $categories_array[$i]['id'] = $category->term_id;
          $categories_array[$i]['name'] = $category->name;
          $i++;
        }
      return $categories_array;
    }

    $cats = cat_drop_down();


    echo '<select data-setting="tab_title">';
    foreach($cats as $cat)
    {
        echo "<option value='".$cat['id']."'>".$cat['name']."</option>";
    }   
    echo '</select>';
0

There is a default wordpress function called . It will return all categoris in a list with category id.

wp_list_categories();
1
  • Thanks but I need to return array not directly output May 25, 2017 at 3:30
0

No, you're not doing anything wrong. Your array of category ids is fine. I understand that you're passing this array to page builder, which in turn, builds form's select dropdown. Values 0,1,2 are simply indexes from your $categories_array. If you still have access to your $categories_array, and the value of selected option is 1, then selected $id = $categories_array[1] - which is 14.

If you were to build your select dropdown, instead of your page builder, it could be like this:

<select data-setting="tab_title">               
   <option>2</option>             
   <option>14</option>                
   <option>1</option>             
</select>

Then you'll get selected category id directly.

If your page builder accepts an array of pairs - id, name, then you'll get select like in @Arun's answer. Of course, you will have to change the code of building your array, to the code suggested by @Arun.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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