【自分用メモ・WordPress】カスタム投稿のカテゴリ取得

WordPressのカスタム投稿のカテゴリを取得する場合のメモ。

リンクありとリンクなしの場合を書いておく。

リンクあり

<?php get_the_term_list( $id, $taxonomy, $before, $sep, $after ) ?> 

/*
$id : 投稿ID(整数, 必須) 
$taxonomy : タクソノミーの名前(文字列, 必須)
$before : 前に入れる文字列(文字列, オプション) 
$sep :  タームを区切る文字列(文字列, オプション)
$after : 後に続く文字列(文字列, オプション) 
*/ 

リンクなし

<?php
   if ($terms = get_the_terms($post->ID, 'カテゴリ名')) {
      foreach ( $terms as $term ) {
        echo esc_html($term->name);
      }
   }
?>

/*
get_the_terms( $id, $taxonomy );

オブジェクトを返す
stdClass Object
(
    [term_id] =>
    [name] =>
    [slug] =>
    [term_group] => 
    [term_order] => 
    [term_taxonomy_id] =>
    [taxonomy] =>
    [description] => 
    [parent] =>
    [count] =>
    [object_id] =>
)

*/

参考サイト

https://wpdocs.osdn.jp/%E9%96%A2%E6%95%B0%E3%83%AA%E3%83%95%E3%82%A1%E3%83%AC%E3%83%B3%E3%82%B9/get_the_term_list