teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

ACFにあわせた回答を追記

2017/12/16 06:24

投稿

退会済みユーザー
answer CHANGED
@@ -2,4 +2,38 @@
2
2
  それから、get_term_meta関数でタームのメタ情報を取得表示するイメージでしょうか。
3
3
 
4
4
  [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](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)
5
- [https://developer.wordpress.org/reference/functions/get_term_meta/](https://developer.wordpress.org/reference/functions/get_term_meta/)
5
+ [https://developer.wordpress.org/reference/functions/get_term_meta/](https://developer.wordpress.org/reference/functions/get_term_meta/)
6
+
7
+ ---
8
+ **2017/12/16 追記**
9
+ ACFプラグインでタクソノミーにカスタムフィールドを設定した場合のカスタムフィールド値を取得するソースです。
10
+ **注意点はACFのタクソノミーのカスタムフィールド値を取得するときにはWordPressのget_term_meta関数が使えないらしいこと。**
11
+ かわりにACFのget_filed関数で取得する必要があります。
12
+ 詳細は公式のドキュメントに「Adding fields to a taxonomy term」ページがあるので、そちらをご参照ください。
13
+
14
+ ```PHP
15
+ $taxonomy_name = "article_taxonomy";
16
+ $customfield_name = "field_name";
17
+
18
+ $terms = get_the_terms(get_the_ID(), $taxonomy_name);
19
+
20
+ var_dump($terms);
21
+
22
+ if($terms) :
23
+ foreach($terms as $term) :
24
+ $objs = get_field($customfield_name, $taxonomy_name."_".$term->term_id);
25
+
26
+ var_dump($objs);
27
+
28
+ if(objs) :
29
+ foreach($objs as $obj) :
30
+ echo $obj->post_title;
31
+ endforeach;
32
+ endif;
33
+ endforeach;
34
+ endif;
35
+ ```
36
+
37
+ 参考URL:
38
+ ACF | Adding fields to a taxonomy term
39
+ [https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/](https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/)