回答編集履歴

1

回答追記(複数タームの場合)

2016/12/14 05:09

投稿

退会済みユーザー
test CHANGED
@@ -26,9 +26,7 @@
26
26
 
27
27
  ```
28
28
 
29
-
30
-
31
- (2)
29
+ (3)
32
30
 
33
31
  エラーの直接の原因はここですね。
34
32
 
@@ -43,3 +41,59 @@
43
41
  $term = array_shift(get_the_terms($post->ID, 'faq_cat')); // タクソノミー名を指定する
44
42
 
45
43
  ```
44
+
45
+ (4)
46
+
47
+ 複数タームの場合はこんな感じです。
48
+
49
+ ```PHP
50
+
51
+ <?php
52
+
53
+ global $post;
54
+
55
+ $terms = get_the_terms($post->ID, 'faq_cat');
56
+
57
+
58
+
59
+ foreach($terms as $term) {
60
+
61
+ echo '<h1>' . $term->name . '</h1>';
62
+
63
+ $args = array(
64
+
65
+ 'numberposts' => 8, //8件表示
66
+
67
+ 'post_type' => 'faq', //カスタム投稿タイプ名
68
+
69
+ 'taxonomy' => 'faq_cat', //タクソノミー名
70
+
71
+ 'term' => $term->slug, //ターム名
72
+
73
+ 'orderby' => 'rand', //ランダム表示
74
+
75
+ 'post__not_in' => array($post->ID) //表示中の記事を除外
76
+
77
+ );
78
+
79
+ ?>
80
+
81
+ <?php $myPosts = get_posts($args); if($myPosts) : ?>
82
+
83
+ <?php foreach($myPosts as $post) : setup_postdata($post); ?>
84
+
85
+ <p><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?><?php the_title(); ?></a></p>
86
+
87
+ <?php endforeach; ?>
88
+
89
+ <?php else : ?>
90
+
91
+ <p>関連アイテムはまだありません。</p>
92
+
93
+ <?php endif; wp_reset_postdata();
94
+
95
+ }
96
+
97
+ ?>
98
+
99
+ ```