カスタムタクソノミーのタームのスラッグをそのままクラス名に使いたくて、投稿の持っている各タームのスラッグ名を一覧表示する方法をご紹介します。
<?php
$terms = wp_get_object_terms($post->ID,'タクソノミー名');
foreach($terms as $term){
echo 't-' . $term->slug . ' ';
}
?>
‘t-‘はわかりやすくするためにつけたものなので、お好みで削除してください。
使用例
- 投稿タイプ:aaaaa
- タクソノミー名:yakumi
- タームスラッグ:wasabi, shoga, karashi
- 投稿1:wasabi, shoga
- 投稿2:karashi
- 投稿3:wasabi, karashi
<ul>
<?php
$args = array();
$args['post_type'] = 'aaaaa';
$the_query = new WP_Query($args);
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li class="
<?php
$terms = wp_get_object_terms($post->ID,'yakumi');
foreach($terms as $term){
echo 't-' . $term->slug . ' ';
}
?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
結果
<ul> <li class="t-wasabi t-shoga">投稿1</li> <li class="t-karashi">投稿2</li> <li class="t-wasabi t-karashi">投稿3</li> </ul>

