0

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}
    />
  • 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 Jul 28 at 13:30
0

Ich have to write

options={getCategories()}

that solved this problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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