简体   繁体   English

如何在选择框选项中获取类别列表

[英]How to get category list in select box option

I want to add options in my control like key => value pair array of all available options 我想在控件中添加所有可用选项的key => value对数组之类的选项

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 但是我想使用标题的所有类别来代替标题说明和按钮,所以我编写了一个函数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 ? 但是我没有类别列表选项, my_cat函数有什么问题吗?

Try by replacing you my_cat() with this: 尝试用以下代码替换my_cat()

function my_cat() {
    $categories = get_categories();
    $cat_array = [];
    foreach ($categories as $category) :
        $cat_array[$category->term_id] = $category->name;
    endforeach;
    return $cat_array;
}

To do this correctly, in opinion, we want choices to take associative array in this form: 鉴于此,正确地做到这一点,我们希望choices采用这种形式的关联数组:

$this->add_control('show_elements', [
    'label' => __('Show Elements', 'your-plugin'),
    'type' => Controls_Manager::SELECT2,
    'choices' => my_cat(), //<-- Check this line.
    'multiple' => true,
        ]
);

Reference: add control 参考: 添加控件

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WordPress-获取类别名称作为选择选项值 - Wordpress - get category name as select option value 在父类别中选择选项时显示子类别列表 - Showing sub-category list when an option select in parent category Laravel 5 在分类编辑页面,列表在顶部分类 select 框中选择 - Laravel 5 on the category edit page, list selected in the top category select box 如何在CodeIgniter中的控制器中获取选择框的选项值 - How to get the option value of the select box in the controller in CodeIgniter 在更新中,如何从选择框中选择一个选项? - in an update, how to select an option from a select box? 在选择框上写上“选择的选项”? - Get “selected option” written on the select box? 如何获取magento中的类别列表? - How to get the category list in magento? 如何从下拉选择框列表中将用户选择的选项的值存储到mysql数据库中? - How can I store the value of a user selected option from a drop down select box list into mysql database? 如何将复选框列表/单选框列表/选择选项列表中的所有选项与选定的选项一起通过php发送到服务器? - How to send all options from a checkbox list/radio box list/ select option list to server along with the selected one by php? 如何在PHP的$ _GET下拉框中选择一个选项 - How can I select an option in a drop down box from $_GET in Php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM