回答編集履歴

1

ACFにあわせた回答を追記

2017/12/16 06:24

投稿

退会済みユーザー
test CHANGED
@@ -7,3 +7,71 @@
7
7
  [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)
8
8
 
9
9
  [https://developer.wordpress.org/reference/functions/get_term_meta/](https://developer.wordpress.org/reference/functions/get_term_meta/)
10
+
11
+
12
+
13
+ ---
14
+
15
+ **2017/12/16 追記**
16
+
17
+ ACFプラグインでタクソノミーにカスタムフィールドを設定した場合のカスタムフィールド値を取得するソースです。
18
+
19
+ **注意点はACFのタクソノミーのカスタムフィールド値を取得するときにはWordPressのget_term_meta関数が使えないらしいこと。**
20
+
21
+ かわりにACFのget_filed関数で取得する必要があります。
22
+
23
+ 詳細は公式のドキュメントに「Adding fields to a taxonomy term」ページがあるので、そちらをご参照ください。
24
+
25
+
26
+
27
+ ```PHP
28
+
29
+ $taxonomy_name = "article_taxonomy";
30
+
31
+ $customfield_name = "field_name";
32
+
33
+
34
+
35
+ $terms = get_the_terms(get_the_ID(), $taxonomy_name);
36
+
37
+
38
+
39
+ var_dump($terms);
40
+
41
+
42
+
43
+ if($terms) :
44
+
45
+ foreach($terms as $term) :
46
+
47
+ $objs = get_field($customfield_name, $taxonomy_name."_".$term->term_id);
48
+
49
+
50
+
51
+ var_dump($objs);
52
+
53
+
54
+
55
+ if(objs) :
56
+
57
+ foreach($objs as $obj) :
58
+
59
+ echo $obj->post_title;
60
+
61
+ endforeach;
62
+
63
+ endif;
64
+
65
+ endforeach;
66
+
67
+ endif;
68
+
69
+ ```
70
+
71
+
72
+
73
+ 参考URL:
74
+
75
+ ACF | Adding fields to a taxonomy term
76
+
77
+ [https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/](https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/)