Zoli
Zoli

Reputation: 1091

Wordpress - Elementor custom widgets - conditional controls

I'm creating my first elementor widget. I need to create 3 conditional controls: Select a (custom)post_type -> select a taxonomy belonging to the respective post_type -> select posts belonging to the respective taxonomy.

How can I create these controlls?

This is what I got untill now, but the problem is, that when I choose a post type, in the taxonomy list I have all the taxonomies, not only those belonging to the chosen post_type.

        $post_types = get_post_types([], 'objects');
        $options = [];
        foreach ( $post_types as $post_type ) {
            $options[$post_type->name] = $post_type->label;
        }
        $this->add_control(
            'post_types',
            [
                'label' => __( 'Choose a post type', 'cf-elementor-apm-widget' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => $options,
                'classes' => 'post_types',
            ]
        );

        $taxonomies = get_taxonomies([], 'objects');

        $options = [];
        foreach ( $taxonomies as $taxonomy ) {
            $options[$taxonomy->name] = $taxonomy->label . " [{$taxonomy->name}]";
        }

        $this->add_control(
            'taxonomies',
            [
                'label' => __( 'Choose a taxonomy', 'cf-elementor-apm-widget' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => $options,
                'classes' => 'taxonomy',
            ]
        );

Upvotes: 1

Views: 1141

Answers (1)

Create various taxonomy selects for each post type. It'll likely work if created programatically so you don't need to hardcode them. Then, only show a particular taxonomy select if the post type select's value matches that. You can do that part with control conditions.

Upvotes: 2

Related Questions