カスタム投稿にタグを色分けして表示する方法。

phpに下記を記載。

これは、タームのスラッグ名とタームの名前を出力せよというコード。
そしてそのクラス名は、label product-各スラッグ名となるので、
label product-丸丸をstyle.cssに記載し、文字色や背景色などを記載すれば表示される。

<?php 
					$terms = get_the_terms( $post->ID, 'type');
					if ( ! is_wp_error( $terms ) && $terms ) :
						foreach ( $terms as $term) :
				 ?>

				 	<span class="label product-<?php echo esc_attr($term->slug); ?>"><?php echo esc_html( $term->name ); ?></span>
				 <?php 
				 	endforeach;
				 endif;
				  ?>



この記事が気に入ったらサポートをしてみませんか?