我如何在元素或自定义小部件中添加 post-类别列表

how can i add post-categories list in elementor custom widget

我的控制码是 我想在 elementor 中创建显示所有 post 类别的控件。请帮助我如何实现这个...

enter image description here


            $this->add_control(
                'show_elements',
                [
                    'label' => __( 'Post Categoris', 'plugin-domain' ),
                    'type' => \Elementor\Controls_Manager::SELECT2,
                    'multiple' => true,
                    'options' => [
                        $category,
                    ],
                    
                ]
            ); ```

您可以使用 WP get_categories() 获取所有类别。检查下面的代码。

$options = array();

$args = array(
    'hide_empty' => false,
);

$categories = get_categories($args);

foreach ( $categories as $key => $category ) {
    $options[$category->term_id] = $category->name;
}

$this->add_control(
    'show_elements',
    [
        'label' => __( 'Post Categoris', 'plugin-domain' ),
        'type' => \Elementor\Controls_Manager::SELECT2,
        'multiple' => true,
        'options' => $options,
    ]
);