最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom taxonomy - How to list posts by terms

programmeradmin3浏览0评论

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>';
}
发布评论

评论列表(0)

  1. 暂无评论