如何在WordPress中显示分类术语中的帖子列表?

如何在WordPress中显示分类术语中的帖子列表?,wordpress,taxonomy,Wordpress,Taxonomy,我想创建如下列表: foreach ($term_list as $term) { $posts = get_posts( [ 'numberposts' => 1, 'category' => $term->ID, ] ); foreach ( $posts as $post ) { echo $post->post_title;

我想创建如下列表:

    foreach ($term_list as $term) {
        $posts = get_posts( [
            'numberposts' => 1,
            'category' => $term->ID,
        ] );

        foreach ( $posts as $post ) {
            echo $post->post_title;
        }
    }
分类术语的名称
-帖子1
-帖子2
-帖子3

到目前为止,我已经迈出了第一步。代码如下:

$term_list = wp_get_post_terms(
        $post->ID,
        'job_listing_category',
        array( 'fields' => 'all' )

    );

这就是分类术语。第一步完成了。但是我如何做类似的事情,并从分类法中获得列表呢

如果是自定义分类术语,您可以尝试:

<?php
$custom_terms = get_terms('post-terms-type');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'post-type',
    'tax_query' => array(
        array(
            'taxonomy' => 'post-terms-type',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
 );
 $loop = new WP_Query($args);
 if($loop->have_posts()) {
    echo '<h2 class="terms-title">'.$custom_term->name.'</h2>';
    echo '<ul class="post-list">';
    while($loop->have_posts()) : $loop->the_post();
        echo '<li><a href="'.get_permalink().'" title="'.get_the_title().'" target="_blank">'.get_the_title().'</a></li>';
    endwhile;
    echo "</ul>";
 }
}
  ?>

使用
get_posts()
可以很容易地完成,如下所示:

    foreach ($term_list as $term) {
        $posts = get_posts( [
            'numberposts' => 1,
            'category' => $term->ID,
        ] );

        foreach ( $posts as $post ) {
            echo $post->post_title;
        }
    }