Elementor中的另一个Select2字段填充Select2字段

发布于 2025-01-18 05:10:41 字数 336 浏览 0 评论 0 原文

我有一个可以使用帖子类型的领域,我希望我的第二个Select2在第一个Select2中填充分类法。

我有一个为两者编写的函数,但是试图从“ post_type”中获取数据很棘手。

以下是在页面构建器中引起的AJAX加载问题。

我已经搜索了高低,但还没有找到解决方案。

提前致谢。

I have a field where I get available post types, and I'd like my second select2 to be populated with taxonomies from the first select2.

I have a function written for both, but trying to get the data from "post_type" is proving tricky.

The below is causing an ajax loading issue in the page builder.

I've searched high and low but haven't found a solution.

Thanks in advance.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

極樂鬼 2025-01-25 05:10:41

我能为您做的最好的是将此github插件插入
它使用JS来做到这一点,

这是该项目的链接:

widget.php:

$this->add_control(
            'post_type',
            [
                'label' => esc_html__( 'Select post type', 'void' ),
                'type' => Controls_Manager::SELECT2,
                'options' => void_grid_post_type(),                                
            ]
        );
        $this->add_control(
            'taxonomy_type',
            [
                'label' => __( 'Select Taxonomy', 'void' ),
                'type' => Controls_Manager::SELECT2,
                'options' => '',                               
            ]
        );

ajax.js

jQuery( function( $ ) {
    elementor.hooks.addAction( 'panel/open_editor/widget', function( panel, model, view ) {

        //get post type 
        $('[data-setting="post_type"]').change(function(){        
            $('[data-setting="taxonomy_type"]').empty();
            var post_type = $('[data-setting="post_type"]').val() || [];
            var data = {
                action: 'void_grid_ajax_tax',
                postTypeNonce : void_grid_ajax.postTypeNonce,
                post_type: post_type
            };        
            $.post(void_grid_ajax.ajaxurl, data, function(response) {        
                var taxonomy_name = JSON.parse(response);                 
                $.each(taxonomy_name,function(){
                    if(this.name == 'post_format'){
                        return;
                    }
                    $('[data-setting="taxonomy_type"]').append('<option value="'+this.name+'">'+this.name+'</option>'); 
                });
                $('[data-setting="taxonomy_type"]')[0].selectedIndex = -1;
            });
            return true;
        });
        $('[data-setting="taxonomy_type"]').change(function(){        
            $('[data-setting="terms"]')[0].options.length = 0;       
            var taxonomy_type = $('[data-setting="taxonomy_type"]').val();
            var data = {
                action: 'void_grid_ajax_terms',
                postTypeNonce : void_grid_ajax.postTypeNonce,
                taxonomy_type: taxonomy_type
            };      
            $.post(void_grid_ajax.ajaxurl, data, function(response) {        
                var terms = JSON.parse(response);                        
                $.each(terms,function(){
                    $('[data-setting="terms"]').append('<option value="'+this.id+'">'+this.name+'</option>'); 
                });
                $('[data-setting="terms"]')[0].selectedIndex = -1;
            });   
            return true;
        });
    } );

});

the best i can do for you is interduce this GitHub plugin
it uses js to do this

here is the link to the project:
https://github.com/WPPlugins/void-elementor-post-grid-addon-for-elementor-page-builder

widget.php:

$this->add_control(
            'post_type',
            [
                'label' => esc_html__( 'Select post type', 'void' ),
                'type' => Controls_Manager::SELECT2,
                'options' => void_grid_post_type(),                                
            ]
        );
        $this->add_control(
            'taxonomy_type',
            [
                'label' => __( 'Select Taxonomy', 'void' ),
                'type' => Controls_Manager::SELECT2,
                'options' => '',                               
            ]
        );

ajax.js

jQuery( function( $ ) {
    elementor.hooks.addAction( 'panel/open_editor/widget', function( panel, model, view ) {

        //get post type 
        $('[data-setting="post_type"]').change(function(){        
            $('[data-setting="taxonomy_type"]').empty();
            var post_type = $('[data-setting="post_type"]').val() || [];
            var data = {
                action: 'void_grid_ajax_tax',
                postTypeNonce : void_grid_ajax.postTypeNonce,
                post_type: post_type
            };        
            $.post(void_grid_ajax.ajaxurl, data, function(response) {        
                var taxonomy_name = JSON.parse(response);                 
                $.each(taxonomy_name,function(){
                    if(this.name == 'post_format'){
                        return;
                    }
                    $('[data-setting="taxonomy_type"]').append('<option value="'+this.name+'">'+this.name+'</option>'); 
                });
                $('[data-setting="taxonomy_type"]')[0].selectedIndex = -1;
            });
            return true;
        });
        $('[data-setting="taxonomy_type"]').change(function(){        
            $('[data-setting="terms"]')[0].options.length = 0;       
            var taxonomy_type = $('[data-setting="taxonomy_type"]').val();
            var data = {
                action: 'void_grid_ajax_terms',
                postTypeNonce : void_grid_ajax.postTypeNonce,
                taxonomy_type: taxonomy_type
            };      
            $.post(void_grid_ajax.ajaxurl, data, function(response) {        
                var terms = JSON.parse(response);                        
                $.each(terms,function(){
                    $('[data-setting="terms"]').append('<option value="'+this.id+'">'+this.name+'</option>'); 
                });
                $('[data-setting="terms"]')[0].selectedIndex = -1;
            });   
            return true;
        });
    } );

});
污味仙女 2025-01-25 05:10:41

我认为可以通过创建您自己的 select2 控件并使用您自己创建的 js 文件传递​​选项来完成 elementor 小部件中的相同行为。

Accomplishing this same behavior within elementor widget I think can be done by creating your own select2 control and passing the options using your own created js file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文