按类别对 WordPress 帖子进行分组

Group WordPress posts by category

经过几次搜索都没有成功,我在这里提出我的问题。 实际上,我正在尝试显示按类别分组的帖子列表:

CAT A

CAT B

这是我试过的代码。 我可以显示类别,但不能显示帖子

<?php 
        $terms = get_terms( 'secteur', array(
            'orderby'    => 'count',
            'hide_empty' => 0
        ) );
        
        foreach( $terms as $term ) {
            $args = array(
                'post_type' => 'client',
                'posts_per_page' => '-1',
              'secteur' => $term->slug
            );
            $query = new WP_Query( $args );
  
            echo'<h3>' . $term->name . '</h3>';
           

                // Start the Loop
                while ( $query->have_posts() ) : $query->the_post(); 
                $secteur_dactivite = get_field( 'secteur_dactivite' );

                echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';

                 endwhile;

            wp_reset_postdata();
 
        } 
    
    ?>

您需要使用 tax_query 作为 WP 查询属性,而不是 secteur。

尝试将其替换为:

$args = array(
  'post_type' => 'client',
  'posts_per_page' => '-1',
  'tax_query' => array(
        array(
            'taxonomy' => 'secteur',
            'field'    => 'slug',
            'terms'    => $term -> slug,
        ),
    ),
);

非常感谢您的回复。 不幸的是,这不会改变显示。显示标题但不显示文章。

<h3>CAT1</h3
<h3>CAT2</h3>
<h3>CAT3</h3>

如果有帮助,我可以使用以下代码显示所有项目:

<?php
            $posts = get_posts(array(
                'numberposts' => -1,
                'post_type' => 'client',
                'post_status' => 'publish'
            ));
            if($posts)
            {
                echo '<div class="row all-item">';
                foreach($posts as $post)
                {
                    echo '<div"><a href="'. get_permalink($post->ID) .'"><img src="'.get_field( 'logo' ).'"></a></div>';
                }
                echo '</div>';
            }
        ?>

非常感谢您的回答。 但对我来说没有任何效果,我也想不出任何解决方案。 我认为问题出在我的分类法配置上。

这是我的自定义 post 类型配置(客户端): https://imgur.com/uDom3PH

我的分类配置(secteur): https://imgur.com/WRwsSbR

我的自定义字段(secteur_dactivite): https://imgur.com/NKQ4GPn

再次感谢您的帮助