如何在 Elementor 免费上显示特定自定义帖子类型类别的帖子?

发布于 2025-01-19 16:31:37 字数 2872 浏览 1 评论 0原文

我正在尝试为自定义帖子类型制作一个 Elementor 模块,分为 2 个文件。

[文件1]用于设置控制。

[文件2]在首页显示自定义帖子类型。

当我将类别直接插入(文件 2)时,一切正常。自定义帖子类型正确显示。可以正确显示按类别等的帖子。

但是,当我尝试处理设置(文件 2)中的类别时,它不起作用。帖子不显示。

如何在 Elementor 中收集自定义帖子类型的类别? 注意:我的自定义帖子类型名称:事件,分类:catevent,类别示例:测试。

这是我的代码:

管理设置中的

    protected function register_controls() {

        $terms = get_terms( array(
            'taxonomy' =>'catevent',
            'hide_empty' => true,
        ) );
    
        $cat_names = array();
        $cat_names = array('all');      
        foreach( $terms as $t ):
            $cat_names[$t->term_id] = $t->name;
        endforeach;
        

        $this->add_control(
                'cat_name',
                    [
                        'label'       => __( 'From Category', 'module-for-elementor' ),
                        'type' => Controls_Manager::SELECT2  ,
                        'multiple' => true,
                        'default' => 'uncategorized',
                        'options' => $cat_names,
                    ]
                    );

代码循环中的

      <?php $blog = array(
       'catevent' => $settings['cat_name'],
       'post_type'         => 'event',
       'post_status'       => 'publish',
       'posts_per_page'    => 1,
       'ignore_sticky_posts' => 1,
       'tax_query' => array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'operator' => 'NOT IN'
        )),
        );
      ?>
      <div class="module1ol">
      <?php $count = 0;
      $the_query = new WP_Query( $blog );
      if($the_query->have_posts()):
      while($the_query->have_posts()): 
      $the_query->the_post();   
    ?>
      <div class="module1-post">
        <div class="cat-color">
          <a class="entry-title" href="<?php the_permalink();?>">
            <?php esc_html_e(wp_trim_words( get_the_title(),8, ''), 'module-for-elementor'); ?>
            </a>
        </div>
      </div>
      <?php  endwhile; ?>
      <?php  wp_reset_query(); 
            endif;?>
    </div>

代码当我像这样直接插入类别时一切正常,但在使用(文件1)中的代码时则不起作用。

    <?php $blog = array(
       'catevent' => 'test',
       'post_type'         => 'event',
       'post_status'       => 'publish',
       'posts_per_page'    => 1,
       'ignore_sticky_posts' => 1,
       'tax_query' => array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'operator' => 'NOT IN'
        )),
        );
      ?>

非常感谢任何人可以提供的帮助。

I am trying to make an Elementor module for Custom Post Type, separate into 2 files.

[File 1] for setting control.

[File 2] to display the custom post type on the front page.

When I insert the Category directly to (File 2), everything works fine. The custom post type is displayed correctly. Can display post by category etc correctly.

But when I am trying to handle the category from the setting (File 2) it did not work. The post doesn't display.

How to collect the custom post type's category in Elementor?
Note: my custom post type name: event, taxonomy: catevent, and the category sample: test.

Here is my code:

Code in admin setting

    protected function register_controls() {

        $terms = get_terms( array(
            'taxonomy' =>'catevent',
            'hide_empty' => true,
        ) );
    
        $cat_names = array();
        $cat_names = array('all');      
        foreach( $terms as $t ):
            $cat_names[$t->term_id] = $t->name;
        endforeach;
        

        $this->add_control(
                'cat_name',
                    [
                        'label'       => __( 'From Category', 'module-for-elementor' ),
                        'type' => Controls_Manager::SELECT2  ,
                        'multiple' => true,
                        'default' => 'uncategorized',
                        'options' => $cat_names,
                    ]
                    );

Code in the Loop

      <?php $blog = array(
       'catevent' => $settings['cat_name'],
       'post_type'         => 'event',
       'post_status'       => 'publish',
       'posts_per_page'    => 1,
       'ignore_sticky_posts' => 1,
       'tax_query' => array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'operator' => 'NOT IN'
        )),
        );
      ?>
      <div class="module1ol">
      <?php $count = 0;
      $the_query = new WP_Query( $blog );
      if($the_query->have_posts()):
      while($the_query->have_posts()): 
      $the_query->the_post();   
    ?>
      <div class="module1-post">
        <div class="cat-color">
          <a class="entry-title" href="<?php the_permalink();?>">
            <?php esc_html_e(wp_trim_words( get_the_title(),8, ''), 'module-for-elementor'); ?>
            </a>
        </div>
      </div>
      <?php  endwhile; ?>
      <?php  wp_reset_query(); 
            endif;?>
    </div>

Everything works fine when I insert the category directly like this but did not work when using code from (File 1).

    <?php $blog = array(
       'catevent' => 'test',
       'post_type'         => 'event',
       'post_status'       => 'publish',
       'posts_per_page'    => 1,
       'ignore_sticky_posts' => 1,
       'tax_query' => array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'operator' => 'NOT IN'
        )),
        );
      ?>

Really appreciate any help anyone can provide.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文