custom taxonomy - How to list posts by terms

I have a custom post type with custom taxonomy and 3 differents terms.I'm trying to build a page with this structu

I have a custom post type with custom taxonomy and 3 differents terms.

I'm trying to build a page with this structure :

Term 1

  • Post tagged with term 1
  • Post tagged with term 1

Term 2

  • Post tagged with term 2
  • Post tagged with term 2
  • Post tagged with term 2

Term 3

  • Post tagged with term 3

Etc...

What is the best way to achieve that ? has_term() ?

I have a custom post type with custom taxonomy and 3 differents terms.

I'm trying to build a page with this structure :

Term 1

  • Post tagged with term 1
  • Post tagged with term 1

Term 2

  • Post tagged with term 2
  • Post tagged with term 2
  • Post tagged with term 2

Term 3

  • Post tagged with term 3

Etc...

What is the best way to achieve that ? has_term() ?

Share Improve this question asked Jul 10, 2020 at 8:31 BakuraBakura 1
Add a comment  | 

1 Answer 1

Reset to default 0
$terms = get_terms('CUSTOM_TAXONOMY', ['hide_empty' => true]);
foreach( $terms as $term ){
    echo '<section>';
    echo '<h1>' . $term->name . '</h1>';

    $posts = get_posts([
        'post_type' => 'CUSTOM_POST_TYPE'
        'tax_query' => [
            [
                'taxonomy'  => 'CUSTOM_TAXONOMY',
                'field'     => 'term_id',
                'terms'     => $term->term_id
            ]
        ]   
    ]);

    echo '<ul>';
    foreach($posts as $post){
            echo sprintf('<li><a href="%s">%s</a></li>', get_permalink($post), $post->post_title);
    }
    echo '</ul>';
    echo '</section>';
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742274989a4413418.html

相关推荐

  • custom taxonomy - How to list posts by terms

    I have a custom post type with custom taxonomy and 3 differents terms.I'm trying to build a page with this structu

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信