好的,如果你想从当前帖子分配到的每个类别中随机显示3篇帖子,那么。。。
首先,你应该循环浏览所有术语,并获得每个术语的随机帖子:
$term_list = wp_get_post_terms( $post->ID, \'listing-category\', array( \'fields\' => \'ids\' ) );
if ( $term_list && ! is_wp_error($term_list) ) {
foreach ( $term_list as $term_id ) {
// get random posts for given term
}
}
目前,您从每个类别中随机获得帖子,因为您的查询有一个小缺陷:
$args = array(
\'post_type\' => \'listing\',
\'category\' => $term_list[0], // <- this works with built-in post categories, not your custom taxonomy called listing-category
\'post_status\' => \'publish\',
\'orderby\' => \'rand\',
\'posts_per_page\' => 3,
);
因此,您应该使用Tax\\u查询:
$args = array(
\'post_type\' => \'listing\',
\'post_status\' => \'publish\',
\'orderby\' => \'rand\',
\'posts_per_page\' => 3,
\'tax_query\' => array(
array( \'taxonomy\' => \'listing-category\', \'field\' => \'term_id\', \'terms\' => <TERM_ID> ), // change <TERM_ID> for real term
// so in your code it\'s $term_list[0] and in my loop just $term_id
)
);