archive.php内で投稿一覧を表示したときにターム一覧を表示し、その投稿に紐づいているタームにはクラスをつける。
ということをしたいのですが、できません。
現在archive.php内でtaxonomy.phpを取得しており
taxonomy.phpでは
php
1<?php // get_terms を使ったターム一覧の表示 2 $taxonomy_terms = get_terms('school_tag'); // タクソノミースラッグを指定 3 if(!empty($taxonomy_terms)&&!is_wp_error($taxonomy_terms)){ 4 echo '<ul>'; 5 foreach($taxonomy_terms as $taxonomy_term): // foreach ループの開始 6?> 7<li><a href="<?php echo get_term_link($taxonomy_term); ?>" class="<?php if($taxonomy_term->slug === $term){ echo 'current'; } ?>"><?php echo $taxonomy_term->name; ?></a></li> 8<?php 9 endforeach; // foreach ループの終了 10 echo '</ul>'; 11 } 12?> 13
としておりますがクラスは付与されません。
html
1<a href="リンク" class>ターム</a>
クラスをつけたい部分にはついておらず、タームやリンクは正常です。
付与されない、とは、具体的にHTML出力ではどの様になっているのでしょうか。
またソース内の「$term」はどこで定義されていますでしょうか。
html出力はクラスがつかない以外は正常に出ています。
$termはtaxonomy.phpは$termで投稿に紐づいてるタームスラッグが出るということでそのまま利用していますが、吐き出したいのは紐づいているタームのスラッグになります。
可能であれば紐づいているタームのスラッグを出したいのですが、
<?php // get_terms を使ったターム一覧の表示
$taxonomy_terms = get_terms('school_tag'); // タクソノミースラッグを指定
if(!empty($taxonomy_terms)&&!is_wp_error($taxonomy_terms)){
echo '<ul>';
foreach($taxonomy_terms as $taxonomy_term): // foreach ループの開始
$items = get_the_terms($post->ID, 'school_tag');
foreach ( $items as $item ) {
$name = $item -> name;
}
echo $taxonomy_term->slug;
echo $name;
?>
<li><a href="<?php echo get_term_link($taxonomy_term); ?>" class="<?php if($taxonomy_term->slug === $items->slug){ echo 'current'; } ?>"><?php echo $taxonomy_term->name; ?></a></li>
<?php
endforeach; // foreach ループの終了
echo '</ul>';
}
?>
としてもクラスの付与はできませんでした。
回答2件
あなたの回答
tips
プレビュー