我正在尝试获取特定自定义类型的所有分类名称 在stackoverflow存档中,我找到了下面的脚本,用于单个postID。 我想循环一个自定义帖子类型的所有分类名称,而不只是一个带有id的分类法名称 Taxomy应该是: my_tax 和自定义帖子类型: my_team
$terms = get_the_terms( $post->ID, 'my_tax' );
foreach($terms as $term) {
echo $term->name;
}
答案 0 :(得分:0)
此类代码可以是:
$type = 'my_team';
$args = array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
);
$my_query = null;
$my_query = new WP_Query($args);
if ($my_query->have_posts()) {
while ($my_query->have_posts()) {
$post = $my_query->the_post();
$terms = get_the_terms( $post->ID, 'my_tax' );
foreach($terms as $term) {
echo $term->name;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().