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

質問編集履歴

6

未解決ですが一部コードを修正と現在の状態

2021/12/28 07:27

投稿

kawausomaru
kawausomaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -65,92 +65,7 @@
65
65
  追記:nekoraさんのコードを試した結果
66
66
 
67
67
  1回目
68
- ```php
69
- <section class="member">
70
- <div class="member__inner">
71
- <?php
72
- $args = array(
73
- 'post_type' => 'member',
74
- 'posts_per_page' => 4, // 表示する件数
75
- 'orderby' => 'date', // 日付でソート
76
- 'order' => 'DESC', // DESCで新しい、ASCで古い順に表示
77
- 'tax_query' => array( //タクソノミーに関する指定はこの中にすべて
78
- 'taxonomy' => 'position',
79
- 'field' => 'slug',
80
- 'terms' => 'position3', //タームをスラッグで指定('field'が'slug'なので)
81
- )
82
- );
83
- $posts = get_posts( $args );
84
- if ( $posts ): ?>
85
- <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
86
- <h3 class="member__inner__title"><?php the_field('position3'); ?></h2>
87
- <div class="member__inner__flex">
88
- <a href="<?php the_permalink(); ?>" class="member__inner__flex__box">
89
- <figure class="member__inner__flex__box__img">
90
- <?php if ( has_post_thumbnail() ): ?>
91
- <?php the_post_thumbnail(); ?>
92
- <?php else: ?>
93
- <img src="<?php echo get_template_directory_uri(); ?>/img/ni1.png" alt="西川カメラ1" alt="" class="">
94
- <?php endif; ?>
95
- </figure>
96
- <h2 class="member__inner__flex__box__name"><?php the_title(); ?></h2>
97
- </a>
98
- </div>
99
- <?php
100
- endforeach;
101
- endif;
102
- wp_reset_postdata();
103
- ?>
104
- </div>
105
- </section>
106
68
 
107
- ```
108
- 3件のカスタム投稿の内容が表示されているがタームの絞り込みは実装されていない状態
109
- ![イメージ説明](a85d38dd309356dbe25a98919a3c53cc.png)
110
-
111
- 2回目
112
-
113
- ```php
114
- <section class="member">
115
- <div class="member__inner">
116
- <?php
117
- $args = array(
118
- 'post_type' => 'member',
119
- 'posts_per_page' => 4, // 表示する件数
120
- 'orderby' => 'date', // 日付でソート
121
- 'order' => 'DESC', // DESCで新しい、ASCで古い順に表示
122
- 'tax_query' => array( //タクソノミーに関する指定はこの中にすべて
123
- array( //タクソノミー指定の時はarrayのarray出ないといけない
124
- 'taxonomy' => 'position',
125
- 'field' => 'slug',
126
- 'terms' => 'position3', //タームをスラッグで指定('field'が'slug'なので)
127
- )
128
- )
129
- );
130
- $posts = get_posts( $args );
131
- if ( $posts ): ?>
132
- <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
133
- <h3 class="member__inner__title"><?php the_field('position3'); ?></h2>
134
- <div class="member__inner__flex">
135
- <a href="<?php the_permalink(); ?>" class="member__inner__flex__box">
136
- <figure class="member__inner__flex__box__img">
137
- <?php if ( has_post_thumbnail() ): ?>
138
- <?php the_post_thumbnail(); ?>
139
- <?php else: ?>
140
- <img src="<?php echo get_template_directory_uri(); ?>/img/ni1.png" alt="西川カメラ1" alt="" class="">
141
- <?php endif; ?>
142
- </figure>
143
- <h2 class="member__inner__flex__box__name"><?php the_title(); ?></h2>
144
- </a>
145
- </div>
146
- <?php
147
- endforeach;
148
- endif;
149
- wp_reset_postdata();
150
- ?>
151
- </div>
152
- </section>
153
- ```
154
69
  消えてしまいました
155
70
  ![イメージ説明](546fbfe518a2bccfe01c40e7147db2fb.png)
156
71
  検証ツール上では何も表示されていない状態です
@@ -272,4 +187,78 @@
272
187
 
273
188
  現在の状態においてどのような問題点が考えられますでしょうか?
274
189
  何度もお手数おかけして申し訳ないです。
190
+ よろしくお願いいたします。
191
+
192
+ 追記5:未解決ですが一部コードを修正と現在の状態
193
+
194
+ tax_queryを一時的にコメントアウトし
195
+
196
+ ```php
197
+ <?php the_field('positionc', $post->ID); ?>
198
+ ```
199
+
200
+ ```php
201
+ <h3 class="member__inner__title">教授</h2>
202
+ ```
203
+ 上記のコードの内容と場所を変更しました。
204
+ これによりposition5(教授のターム)で絞り込もうとしたのですが
205
+ 表示されず。
206
+
207
+ ```php
208
+ <section class="member">
209
+ <div class="member__inner">
210
+ <h3 class="member__inner__title">教授</h2>
211
+ <div class="member__inner__flex">
212
+ <?php
213
+ $args = array(
214
+ 'post_type' => 'member',
215
+ 'posts_per_page' => 4, // 表示する件数
216
+ 'orderby' => 'date', // 日付でソート
217
+ 'order' => 'DESC', // DESCで新しい、ASCで古い順に表示
218
+ 'tax_query' => array( //タクソノミーに関する指定はこの中にすべて
219
+ array( //タクソノミー指定の時はarrayのarrayでないといけない
220
+ 'taxonomy' => 'position',
221
+ 'field' => 'slug',
222
+ 'terms' => array('position5'), //タームズなのでスラッグをarrayで指定。カンマで区切れば複数指定できるので汎用性が高い('field'が'slug'なので)
223
+ )
224
+ )
225
+
226
+ );
227
+ $posts = get_posts( $args );
228
+ if ( $posts ): ?>
229
+ <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
230
+ <a href="<?php echo get_permalink($post->ID); ?>" class="member__inner__flex__box">
231
+ <figure class="member__inner__flex__box__img">
232
+ <?php if ( has_post_thumbnail($post->ID) ): ?>
233
+ <?php echo get_the_post_thumbnail($post->ID); ?>
234
+ <?php else: ?>
235
+ <img src="<?php echo get_template_directory_uri(); ?>/img/ni1.png" alt="西川カメラ1" alt="" class="">
236
+ <?php endif; ?>
237
+ </figure>
238
+ <h2 class="member__inner__flex__box__name"><?php echo get_the_title($post->ID); ?></h2>
239
+ </a>
240
+ <?php endforeach;?>
241
+ <?php else:?>
242
+ <!-- 要素が取れなかった時もそれがはっきり分かるようにif文のelseでその旨を出力するようにしたほうがいいです -->
243
+ <P>該当する記事はありません</p>
244
+ <?php
245
+ endif;
246
+ wp_reset_postdata();
247
+ ?>
248
+ </div>
249
+
250
+ </div>
251
+ </section>
252
+ ```
253
+ ![イメージ説明](f63badb99634f289318db0c77a2e1738.png)
254
+ tax_queryをコメントアウトした所全記事が表示されました。
255
+ ![イメージ説明](8cf85902356b4c69369648a9f4dd7d1b.png)
256
+ 前回掲載した役職のカスタムフィールドの下半分が見切れていたので載せます。
257
+ ![イメージ説明](b155b1cf855c44d8ff167c07d0388380.png)
258
+ 返り値関連に解決の糸口があるのではないかと調べているのですが
259
+ 未だ解決に至っていない現状です。
260
+ カスタムフィールドはAdvanced Custom Fieldsを使用しております。
261
+
262
+ 現在の状態において考えられる問題点もしくは別の解決手段などありましたら助言して頂けると嬉しいです。
263
+ 何度もお手数おかけして申し訳ないです。
275
264
  よろしくお願いいたします。

5

タームの紐付けの問題点について

2021/12/28 07:27

投稿

kawausomaru
kawausomaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -209,4 +209,67 @@
209
209
  ```
210
210
 
211
211
  ![イメージ説明](cb1c5cd2f043f367ae3e9bd4e5669fa6.png)
212
- 表示されませんでした
212
+ 表示されませんでした
213
+
214
+ 追記4:タームの紐付けの問題点が知りたいです
215
+
216
+ ![イメージ説明](2b8d213968fe8a5b0dec33687de86b57.png)
217
+
218
+ 上記の画像の通りにカスタムフィールドを作り投稿画面に表示させていたので
219
+ 出力させたい役職のフィード名(positionc)をコードに入力しました。
220
+ ```php
221
+ <section class="member">
222
+ <div class="member__inner">
223
+ <?php
224
+ $args = array(
225
+ 'post_type' => 'member',
226
+ 'posts_per_page' => 4, // 表示する件数
227
+ 'orderby' => 'date', // 日付でソート
228
+ 'order' => 'DESC', // DESCで新しい、ASCで古い順に表示
229
+ 'tax_query' => array( //タクソノミーに関する指定はこの中にすべて
230
+ array( //タクソノミー指定の時はarrayのarrayでないといけない
231
+ 'taxonomy' => 'position',
232
+ 'field' => 'slug',
233
+ 'terms' => array('position3'), //タームズなのでスラッグをarrayで指定。カンマで区切れば複数指定できるので汎用性が高い('field'が'slug'なので)
234
+ )
235
+ )
236
+ );
237
+ $posts = get_posts( $args );
238
+ if ( $posts ): ?>
239
+ <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
240
+ <h3 class="member__inner__title"><?php the_field('positionc', $post->ID); ?></h2>
241
+ <div class="member__inner__flex">
242
+ <a href="<?php echo get_permalink($post->ID); ?>" class="member__inner__flex__box">
243
+ <figure class="member__inner__flex__box__img">
244
+ <?php if ( has_post_thumbnail($post->ID) ): ?>
245
+ <?php echo get_the_post_thumbnail($post->ID); ?>
246
+ <?php else: ?>
247
+ <img src="<?php echo get_template_directory_uri(); ?>/img/ni1.png" alt="西川カメラ1" alt="" class="">
248
+ <?php endif; ?>
249
+ </figure>
250
+ <h2 class="member__inner__flex__box__name"><?php echo get_the_title($post->ID); ?></h2>
251
+ </a>
252
+ </div>
253
+ <?php endforeach;?>
254
+ <?php else:?>
255
+ <!-- 要素が取れなかった時もそれがはっきり分かるようにif文のelseでその旨を出力するようにしたほうがいいです -->
256
+ <P>該当する記事はありません</p>
257
+ <?php
258
+ endif;
259
+ wp_reset_postdata();
260
+ ?>
261
+ </div>
262
+ </section>
263
+
264
+ ```
265
+
266
+
267
+ ![イメージ説明](f3c2b53c31ddde649b0d2b7491f29901.png)
268
+
269
+ タームの紐付け作業は上記の画像の通り、役職が表示されており選択できるので
270
+ 問題ないと思ったのですがカウントの数が0のままになります。
271
+ ![イメージ説明](5b5f49ebbf23936cb950701c3c0bebe6.png)
272
+
273
+ 現在の状態においてどのような問題点が考えられますでしょうか?
274
+ 何度もお手数おかけして申し訳ないです。
275
+ よろしくお願いいたします。

4

nekoraさんのコード添削反映

2021/12/26 03:45

投稿

kawausomaru
kawausomaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -159,4 +159,54 @@
159
159
  追記2:カスタム投稿、カスタムタクソノミーの設定画面
160
160
  今回上記を実装するにあたってプラグイン(Custom Post Type UI)を使用したので設定画面を載せます
161
161
  ![イメージ説明](b499cdc2eb9a39216c1f416c2587e28d.png)
162
- ![イメージ説明](6d07adbca34a78a2f733644cb61f74dc.png)
162
+ ![イメージ説明](6d07adbca34a78a2f733644cb61f74dc.png)
163
+
164
+ 追記3:nekoraさんのコードを試した結果
165
+
166
+ ```php
167
+ <section class="member">
168
+ <div class="member__inner">
169
+ <?php
170
+ $args = array(
171
+ 'post_type' => 'member',
172
+ 'posts_per_page' => 4, // 表示する件数
173
+ 'orderby' => 'date', // 日付でソート
174
+ 'order' => 'DESC', // DESCで新しい、ASCで古い順に表示
175
+ 'tax_query' => array( //タクソノミーに関する指定はこの中にすべて
176
+ array( //タクソノミー指定の時はarrayのarrayでないといけない
177
+ 'taxonomy' => 'position',
178
+ 'field' => 'slug',
179
+ 'terms' => array('position3'), //タームズなのでスラッグをarrayで指定。カンマで区切れば複数指定できるので汎用性が高い('field'が'slug'なので)
180
+ )
181
+ )
182
+ );
183
+ $posts = get_posts( $args );
184
+ if ( $posts ): ?>
185
+ <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
186
+ <h3 class="member__inner__title"><?php the_field('position3', $post->ID); ?></h2>
187
+ <div class="member__inner__flex">
188
+ <a href="<?php echo get_permalink($post->ID); ?>" class="member__inner__flex__box">
189
+ <figure class="member__inner__flex__box__img">
190
+ <?php if ( has_post_thumbnail($post->ID) ): ?>
191
+ <?php echo get_the_post_thumbnail($post->ID); ?>
192
+ <?php else: ?>
193
+ <img src="<?php echo get_template_directory_uri(); ?>/img/ni1.png" alt="西川カメラ1" alt="" class="">
194
+ <?php endif; ?>
195
+ </figure>
196
+ <h2 class="member__inner__flex__box__name"><?php echo get_the_title($post->ID); ?></h2>
197
+ </a>
198
+ </div>
199
+ <?php endforeach;?>
200
+ <?php else:?>
201
+ <!-- 要素が取れなかった時もそれがはっきり分かるようにif文のelseでその旨を出力するようにしたほうがいいです -->
202
+ <P>該当する記事はありません</p>
203
+ <?php
204
+ endif;
205
+ wp_reset_postdata();
206
+ ?>
207
+ </div>
208
+ </section>
209
+ ```
210
+
211
+ ![イメージ説明](cb1c5cd2f043f367ae3e9bd4e5669fa6.png)
212
+ 表示されませんでした

3

カスタム投稿、カスタムタクソノミーの設定画面の追加

2021/12/24 02:17

投稿

kawausomaru
kawausomaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -154,4 +154,9 @@
154
154
  消えてしまいました
155
155
  ![イメージ説明](546fbfe518a2bccfe01c40e7147db2fb.png)
156
156
  検証ツール上では何も表示されていない状態です
157
- ![イメージ説明](a51a250d1aaccad8a821ffd0d54cfa3f.png)
157
+ ![イメージ説明](a51a250d1aaccad8a821ffd0d54cfa3f.png)
158
+
159
+ 追記2:カスタム投稿、カスタムタクソノミーの設定画面
160
+ 今回上記を実装するにあたってプラグイン(Custom Post Type UI)を使用したので設定画面を載せます
161
+ ![イメージ説明](b499cdc2eb9a39216c1f416c2587e28d.png)
162
+ ![イメージ説明](6d07adbca34a78a2f733644cb61f74dc.png)

2

訂正後の一回目のコードを追記しました

2021/12/23 11:48

投稿

kawausomaru
kawausomaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -65,6 +65,46 @@
65
65
  追記:nekoraさんのコードを試した結果
66
66
 
67
67
  1回目
68
+ ```php
69
+ <section class="member">
70
+ <div class="member__inner">
71
+ <?php
72
+ $args = array(
73
+ 'post_type' => 'member',
74
+ 'posts_per_page' => 4, // 表示する件数
75
+ 'orderby' => 'date', // 日付でソート
76
+ 'order' => 'DESC', // DESCで新しい、ASCで古い順に表示
77
+ 'tax_query' => array( //タクソノミーに関する指定はこの中にすべて
78
+ 'taxonomy' => 'position',
79
+ 'field' => 'slug',
80
+ 'terms' => 'position3', //タームをスラッグで指定('field'が'slug'なので)
81
+ )
82
+ );
83
+ $posts = get_posts( $args );
84
+ if ( $posts ): ?>
85
+ <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
86
+ <h3 class="member__inner__title"><?php the_field('position3'); ?></h2>
87
+ <div class="member__inner__flex">
88
+ <a href="<?php the_permalink(); ?>" class="member__inner__flex__box">
89
+ <figure class="member__inner__flex__box__img">
90
+ <?php if ( has_post_thumbnail() ): ?>
91
+ <?php the_post_thumbnail(); ?>
92
+ <?php else: ?>
93
+ <img src="<?php echo get_template_directory_uri(); ?>/img/ni1.png" alt="西川カメラ1" alt="" class="">
94
+ <?php endif; ?>
95
+ </figure>
96
+ <h2 class="member__inner__flex__box__name"><?php the_title(); ?></h2>
97
+ </a>
98
+ </div>
99
+ <?php
100
+ endforeach;
101
+ endif;
102
+ wp_reset_postdata();
103
+ ?>
104
+ </div>
105
+ </section>
106
+
107
+ ```
68
108
  3件のカスタム投稿の内容が表示されているがタームの絞り込みは実装されていない状態
69
109
  ![イメージ説明](a85d38dd309356dbe25a98919a3c53cc.png)
70
110
 

1

CSSのタグの削除、nekoraさんの訂正後のコードを反映した結果

2021/12/23 07:11

投稿

kawausomaru
kawausomaru

スコア29

title CHANGED
File without changes
body CHANGED
@@ -59,4 +59,59 @@
59
59
 
60
60
  以上を試したができませんでした。
61
61
  他にも必要な情報などありましたら教えて頂けると助かります。
62
- よろしくおねがいします。
62
+ よろしくおねがいします。
63
+
64
+
65
+ 追記:nekoraさんのコードを試した結果
66
+
67
+ 1回目
68
+ 3件のカスタム投稿の内容が表示されているがタームの絞り込みは実装されていない状態
69
+ ![イメージ説明](a85d38dd309356dbe25a98919a3c53cc.png)
70
+
71
+ 2回目
72
+
73
+ ```php
74
+ <section class="member">
75
+ <div class="member__inner">
76
+ <?php
77
+ $args = array(
78
+ 'post_type' => 'member',
79
+ 'posts_per_page' => 4, // 表示する件数
80
+ 'orderby' => 'date', // 日付でソート
81
+ 'order' => 'DESC', // DESCで新しい、ASCで古い順に表示
82
+ 'tax_query' => array( //タクソノミーに関する指定はこの中にすべて
83
+ array( //タクソノミー指定の時はarrayのarray出ないといけない
84
+ 'taxonomy' => 'position',
85
+ 'field' => 'slug',
86
+ 'terms' => 'position3', //タームをスラッグで指定('field'が'slug'なので)
87
+ )
88
+ )
89
+ );
90
+ $posts = get_posts( $args );
91
+ if ( $posts ): ?>
92
+ <?php foreach ( $posts as $post ) : setup_postdata( $post ); ?>
93
+ <h3 class="member__inner__title"><?php the_field('position3'); ?></h2>
94
+ <div class="member__inner__flex">
95
+ <a href="<?php the_permalink(); ?>" class="member__inner__flex__box">
96
+ <figure class="member__inner__flex__box__img">
97
+ <?php if ( has_post_thumbnail() ): ?>
98
+ <?php the_post_thumbnail(); ?>
99
+ <?php else: ?>
100
+ <img src="<?php echo get_template_directory_uri(); ?>/img/ni1.png" alt="西川カメラ1" alt="" class="">
101
+ <?php endif; ?>
102
+ </figure>
103
+ <h2 class="member__inner__flex__box__name"><?php the_title(); ?></h2>
104
+ </a>
105
+ </div>
106
+ <?php
107
+ endforeach;
108
+ endif;
109
+ wp_reset_postdata();
110
+ ?>
111
+ </div>
112
+ </section>
113
+ ```
114
+ 消えてしまいました
115
+ ![イメージ説明](546fbfe518a2bccfe01c40e7147db2fb.png)
116
+ 検証ツール上では何も表示されていない状態です
117
+ ![イメージ説明](a51a250d1aaccad8a821ffd0d54cfa3f.png)