admin管理员组

文章数量:1327843

I would like to have a multi select field for my categories. How can i do that?

My code doesn't work:

    function getCategories () {
        var options = [];
        var categories = wp.data.select('core').getEntityRecords('taxonomy', 'category');
        categories.forEach((category) => {
            options.push({ value: category.id, label: category.name });
        });
    }

    <SelectControl
        multiple
        label={__('Select a Post')}
        help={__('Select a post to display as a banner.')}
        options={getCategories}
    />

I would like to have a multi select field for my categories. How can i do that?

My code doesn't work:

    function getCategories () {
        var options = [];
        var categories = wp.data.select('core').getEntityRecords('taxonomy', 'category');
        categories.forEach((category) => {
            options.push({ value: category.id, label: category.name });
        });
    }

    <SelectControl
        multiple
        label={__('Select a Post')}
        help={__('Select a post to display as a banner.')}
        options={getCategories}
    />
Share Improve this question edited Jul 28, 2020 at 13:43 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Jul 28, 2020 at 12:54 180690180690 1812 silver badges9 bronze badges 1
  • What's the problem you're trying to solve by implementing this? When you say it doesn't work, what does it do instead? Your code looks incomplete, can you update your question to include the full code in its entirety for implementing this? Where are you hoping to put the control? Is it going in a panel? A custom sidebar? A block? It's not obvious where you've put the SelectControl, and the component references choosing posts not categories. Use the edit link under the tags to answer as many of those questions as possible. How are you intending to save the values? – Tom J Nowell Commented Jul 28, 2020 at 13:30
Add a comment  | 

1 Answer 1

Reset to default 0

Ich have to write

options={getCategories()}

that solved this problem.

本文标签: block editorCategories in Gutenberg