请尝试以下代码,以从每个术语中仅获取一个自定义帖子。用中的术语ID替换术语ID$array_term = array(16,21);
# pass your term IDS
$array_term = array(16,21);
$terms_array = array(
\'taxonomy\' => \'categories\',// you can change it according to your taxonomy
\'include\' => $array_term,
\'parent\' => 0 // If parent => 0 is passed, only top-level terms will be returned
);
$categories_terms = get_terms($terms_array);
foreach($categories_terms as $categorie_term): ?>
<h4><?php echo $categorie_term->name; ?></h4>
<?php
$post_args = array(
\'posts_per_page\' => 1,
\'post_type\' => \'book\', // you can change it according to your custom post type
\'tax_query\' => array(
array(
\'taxonomy\' => \'categories\', // you can change it according to your taxonomy
\'field\' => \'term_id\', // this can be \'term_id\', \'slug\' & \'name\'
\'terms\' => $categorie_term->term_id,
)
)
);
$query = new WP_Query($post_args);
if ( $query->have_posts() )
{
while ( $query->have_posts() )
{
$query->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php
}
}
wp_reset_postdata();
endforeach; // End Term foreach;
让我知道这对你是否有效