---
title: "特定の投稿に紐づくターム情報の取得"
description: "覚書メモ カスタム投稿Aとカスタム投稿Bの両方にカスタムタームの情報を持たせたり…"
url: "https://www.sensitivity.jp/wordpress/%e7%89%b9%e5%ae%9a%e3%81%ae%e6%8a%95%e7%a8%bf%e3%81%ab%e7%b4%90%e3%81%a5%e3%81%8f%e3%82%bf%e3%83%bc%e3%83%a0%e6%83%85%e5%a0%b1%e3%81%ae%e5%8f%96%e5%be%97/"
canonical: "https://www.sensitivity.jp/wordpress/%e7%89%b9%e5%ae%9a%e3%81%ae%e6%8a%95%e7%a8%bf%e3%81%ab%e7%b4%90%e3%81%a5%e3%81%8f%e3%82%bf%e3%83%bc%e3%83%a0%e6%83%85%e5%a0%b1%e3%81%ae%e5%8f%96%e5%be%97/"
date: "2020-06-28"
modified: "2020-06-28"
categories: ["WordPress"]
---

# 特定の投稿に紐づくターム情報の取得

覚書メモ

カスタム投稿Aとカスタム投稿Bの両方にカスタムタームの情報を持たせたり、 ターム情報を元に色んなことをすることが増えてきた為の覚書メモ。

本当に、、、、、、、、
 このタームの出力方法が出したいテンプレートやループの方法によって違うので混乱するんです（涙

## single.php等で表示している投稿に紐づく
タームを表示する場合

まずは、どんな情報が入っていて何で呼び出せるか確認。

```

$terms = get_the_terms($post->ID,'custom_trem');
foreach( $terms as $term ) {
echo $term->term_id; // タームID
echo $term->name; // 名前
echo $term->slug; // スラッグ
echo $term->term_group; // タームグループ
echo $term->term_order; // タームオブジェクト
echo $term->term_taxonomy_id; // タームタクソノミーID
echo $term->taxonomy; // タクソノミー
echo $term->description; // ディスクリプション
echo $term->parent; // 親
echo $term->count; // カウント
echo $term->object_id; // オブジェクトID
}
```

※’custom_trem’は使用しているタスタムタクソノミー名

### 何が取得されているのか確認出来たら、
そのページで使用したい情報を表示出来るように記述。

```

$terms = get_the_terms($post->ID, 'custom_trem');
if ( $terms ) {
	echo '<ul>';
	foreach ( $terms as $term ) {
		$term_link = get_term_link( $term );
		echo '<li><a href="'.esc_url( $term_link ).'">'.$term->name.'</a></li>';
	}
	echo '</ul>';
}
```

そうそう、、、これを出力させたかった・・・・・。

[関数リファレンス/get the terms:WordPress Codex 日本語版](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_terms)

