0

I need to use an array of existing categories as a dropdown option value then use the selected category ID. More exactly, I try to make a custom module to display the latest posts form selected category using Visual Composer plugin tutorial.

I tried to use wp_dropdown_categories in the follwoing code but I get some errors and nothing works:

        array(
        'param_name'    => 'category_id',
        'type'          => 'dropdown',
        'value'         => wp_dropdown_categories, // here I'm stuck
        'heading'       => __('Category filter:', 'overmax'),
        'description'   => '',
        'holder'        => 'div',
        'class'         => ''
        ),

Thanks

2 Answers 2

6

Try this,

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

array(
  'param_name'    => 'category_id',
  'type'          => 'dropdown',
  'value'         => $categories_array, // here I'm stuck
  'heading'       => __('Category filter:', 'overmax'),
  'description'   => '',
  'holder'        => 'div',
  'class'         => ''
),
0
4

In addtion to the answer, if you want that you dropdown value has the id of the category you should do something like this:

$categories_array = array();
$categories = get_terms('tax_id', array('hide_empty' => false));
foreach( $categories as $category ){
  $categories_array[$category->name] = $category->term_id; 
}

Your Answer

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

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