I want to add options in my control like key => value
pair array of all available options
Like this:
$this->add_control('show_elements', [
'label' => __('Show Elements', 'your-plugin'),
'type' => Controls_Manager::SELECT2,
'options' => [
'title' => __('Title', 'your-plugin'),
'description' => __('Description', 'your-plugin'),
'button' => __('Button', 'your-plugin'),
],
'multiple' => true,
]
);
But in place of title description and button I want to have all the categories of my post so I write a function my_cat
function my_cat() {
$categories = get_categories();
echo '[';
foreach ($categories as $category) :
echo $category->term_id . '=>' . $category->name . ',';
endforeach;
echo ']';
}
And I use it for options
$this->add_control('show_elements', [
'label' => __('Show Elements', 'your-plugin'),
'type' => Controls_Manager::SELECT2,
'options' => my_cat(),
'multiple' => true,
]
);
But I'm not getting option with category list, is there anything wrong with my_cat
function ?
Try by replacing you
my_cat()
with this:To do this correctly, in opinion, we want
choices
to take associative array in this form:Reference: add control
Hope this helps!