WordPressの記事に関連付けられたタグの一覧を表示してくれるthe_tag。
そのタグ一覧から特定のタグを除外/削除する場合はthe_tagでは難しそう。しかしget_the_tagsを使うのであれば可能なことが分かったのでメモ。ループ内で使っています。
参考にしたのは下のQ&A。
参照: Exclude a specific tag from the get_the_tags list – WordPress Development Stack Exchange
<?php $tags = get_the_tags( $post->ID ); $separator = ', '; $output = ''; if($tags){ foreach($tags as $tag) { if($tag->slug != "thistag"){ // 除外するタグ(この場合はスラッグ) $output .= '<a href="'.get_tag_link( $tag->term_id ).'">'.$tag->name.'</a>'.$separator; } } echo trim($output, $separator); } ?>
また、セレクトボックスにタグ一覧を表示させる場合、特定のタグを除外/削除するには下記のようになります。
<?php $tags = get_tags('exclude=5'); if ( $tags ) : ?> <!-- 除外するタグ(ID) --> <select onchange="document.location.href=this.options[this.selectedIndex].value;" class="custom-select form-control-sm"> <?php foreach ( $tags as $tag ): ?> <option value="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>"><?php echo esc_html( $tag->name ); ?></option> <?php endforeach; ?> </select> <?php endif; ?>
2020年5月12日追記:
WordPressで非推奨となった
clean_url
を esc_url
に、wp_specialchars
を esc_html
へ変更いたしました。