カテゴリー
wordpress テンプレートタグ

【wordpress】所属しているターム名を表示する方法

表示されている記事の所属しているターム名(カテゴリー名)を表示させる方法。

・所属しているターム名(カテゴリー名)をひとつ表示

<?php $terms = get_the_terms($post->ID, 'taxonomy名'); foreach($terms as $term){ $term_name = $term->name; echo $term_name; break; }; ?>

・所属しているターム名(カテゴリー名)一覧を表示(リンク有り)

<?php echo get_the_term_list( $post->ID, 'taxonomy名', '<div>', '区切り部分のhtml', '</div>'); ?>

・所属しているターム名(カテゴリー名)一覧を表示(リンクなし)

<?php $terms = get_the_terms($post->ID, 'taxonomy名'); $t_slug=''; foreach($terms as $term){ $term_name = $term->name; $t_slug = $t_slug.$term_name.'区切り部分のhtml'; }; $t_slug = substr($t_slug, 0, -1); echo $t_slug; ?>

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です