質問編集履歴

1

wp_queryについて

2021/07/11 14:19

投稿

homepage-site
homepage-site

スコア28

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- wordpressの自作テーマの固定ページのページネーションにつ
3
+ wordpressの自作テーマの固定ページのページネーションで1ページ目以降のコンテンツが変わらな
4
4
 
5
5
 
6
6
 
@@ -8,15 +8,11 @@
8
8
 
9
9
 
10
10
 
11
- syntax error, unexpected end of file, expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF)
11
+ なし
12
-
13
-
14
-
15
-
16
-
12
+
13
+
14
+
17
- ### 該当のソースコード
15
+ ```ここに言語を入力
18
-
19
-
20
16
 
21
17
  <?php
22
18
 
@@ -32,423 +28,205 @@
32
28
 
33
29
  <?php get_header(); ?>
34
30
 
35
- <div class="pagination">
36
-
37
- <div class="list-box">
38
-
39
- <ul>
40
-
41
- <?php
42
-
43
- $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
44
-
45
- $the_query = new WP_Query( array(
46
-
47
- 'post_status' => 'publish',
48
-
49
- 'post_type' => 'post', // ページの種類(例、page、post、カスタム投稿タイプ名)
50
-
51
- 'paged' => $paged,
52
-
53
- 'posts_per_page' => 1, // 表示件数
54
-
55
- 'orderby' => 'date',
56
-
57
- 'order' => 'DESC'
58
-
59
- ) );
60
-
61
- if ($the_query->have_posts()) :
62
-
63
- while ($the_query->have_posts()) : $the_query->the_post();
64
-
65
- ?>
66
-
67
-    <main>
68
-
69
-   //ここにメイン部分のコードを記述
70
-
71
- <!-- 指定カテゴリの最新記事読み込み -->
72
-
73
- //読み込みたい内容の指定
74
-
75
- <?php $args = array(
76
-
77
- 'posts_per_page' => 2,
78
-
79
- 'orderby' => 'date',
80
-
81
-
82
-
83
- );
84
-
85
-
86
-
87
- $posts_array = get_posts( $args );
88
-
89
-
90
-
91
- if (!empty($posts)):
92
-
93
- ?>
94
-
95
-
96
-
97
- //記事がある場合に表示
98
-
99
- PHP
100
-
101
- <div class="post__pagination">
102
-
103
- <?php $nextpost = get_adjacent_post(false, '', false); if ($nextpost) : ?>
104
-
105
- <ul class="newarrival_list">
106
-
107
- <?php
108
-
109
- //記事データを一つづつ表示処理
110
-
111
- foreach($posts_array as $post):
112
-
113
- setup_postdata($posts_array);
114
-
115
- ?>
116
-
117
- <li class="custom"><a href="<?php echo get_permalink($nextpost->ID); ?>">
118
-
119
- <div class="img-wrap">
120
-
121
- <!--画像を追加-->
122
-
123
- <?php
124
-
125
- // アイキャッチ画像の確認
126
-
127
- if ( has_post_thumbnail()) {
128
-
129
- // 存在する
130
-
131
- the_post_thumbnail($nextpost->ID);
132
-
133
- }
134
-
135
- else {
136
-
137
- // 存在しない
138
-
139
- echo '<img src="noimage.jpg">';
140
-
141
- }
142
-
143
- ?></div>
144
-
145
- <!--リンククラスつきのタイトルを追加-->
146
-
147
- <div class="img-wrap2">
148
-
149
- <h2><?php the_title($nextpost->post_title); ?></h2></div>
150
-
151
- <!--日付けを追加-->
152
-
153
-   <div class="img-wrap3">
154
-
155
- <?php the_time('Y/m/d', $nextpost->ID); ?></div>
156
-
157
- <!--リンククラス付きのカテゴリーを追加-->
158
-
159
- <?php
160
-
161
- $categories = get_the_category($nextpost->ID);
162
-
163
- if ( $categories ) {
164
-
165
- echo '<ul>';
166
-
167
- foreach ( $categories as $category ) {
168
-
169
- echo '<li class="cat1"'.$category->slug.'"><a href="'.esc_url(get_category_link($category->term_id)).'">'.$category->name.'</a></li>';
170
-
171
- }
172
-
173
- echo '</ul>';
174
-
175
- }
176
-
177
- ?>
178
-
179
-   <!--リンククラス付きのコメント数を追加-->
180
-
181
- <?php $num_comments = get_comments_number($nextpost->ID);
182
-
183
- if ( $num_comments == 0 ) {
184
-
185
- $comments = __('No Comments'); // 댓글이 없을 경우
186
-
187
- } elseif ( $num_comments > 1 ) {
188
-
189
- $comments = $num_comments . __(' Comments'); // 댓글이 2개 이상일 경우
190
-
191
- } else {
192
-
193
- $comments = __('1 Comment'); // 댓글이 1개일 경우
194
-
195
- }
196
-
197
- $write_comments = '<span class="singlecomments"><a href="' . get_comments_link() .'">'. $comments.'</a></span>';
198
-
199
- echo $write_comments;
200
-
201
- ?>
202
-
203
- <!--リンククラス付きのコメントの抜粋を追加-->
204
-
205
- <div class="img-wrap4">
206
-
207
- <?php the_excerpt($nextPost->ID); ?></div>
208
-
209
- <!--リンククラス付きの記事を読むを追加-->
210
-
211
- <div class="img-wrap5">
212
-
213
- <a href="<?php the_permalink($nextPost->ID); ?>">記事を読む</a></div>
214
-
215
- <?php endforeach; ?>
216
-
217
- </ul>
218
-
219
-
220
-
221
- <?php
222
-
223
- //記事データがない場合の処理
224
-
225
- else:
31
+
32
+
33
+ <?php
34
+
35
+ $paged = get_query_var('paged');
36
+
37
+ $wp_query = new WP_Query();
38
+
39
+ $my_posts = array(
40
+
41
+ 'post_type' => "post",//投稿タイプ設定
42
+
43
+ 'posts_per_page' => get_option('posts_per_page'),
44
+
45
+ 'paged' => get_query_var( 'paged', 1 )
46
+
47
+ );
48
+
49
+
50
+
51
+ $wp_query->query( $my_posts );
52
+
53
+ if( $wp_query->have_posts() ): while( $wp_query->have_posts() ) : $wp_query->the_post();
54
+
55
+ ?>
56
+
57
+
58
+
59
+ <?php /**** ▼1件分の投稿内容HTML ****/ ?>
60
+
61
+ <ul class="newarrival_list">
62
+
63
+ <li class="custom"><a href="<?php echo get_permalink(); ?>">
64
+
65
+ <div class="img-wrap">
66
+
67
+ <!--画像を追加-->
68
+
69
+ <?php
70
+
71
+ // アイキャッチ画像の確認
72
+
73
+ if ( has_post_thumbnail()) {
74
+
75
+ // 存在する
76
+
77
+ the_post_thumbnail();
78
+
79
+ }
80
+
81
+ else {
82
+
83
+ // 存在しない
84
+
85
+ echo '<img src="noimage.jpg">';
86
+
87
+ }
88
+
89
+ ?></div>
90
+
91
+ <!--リンククラスつきのタイトルを追加-->
92
+
93
+ <div class="img-wrap2">
94
+
95
+ <h2><?php the_title(); ?></h2></div>
96
+
97
+ <!--日付けを追加-->
98
+
99
+   <div class="img-wrap3">
100
+
101
+ <?php the_time('Y/m/d'); ?></div>
102
+
103
+ <!--リンククラス付きのカテゴリーを追加-->
104
+
105
+ <?php
106
+
107
+ $categories = get_the_category();
108
+
109
+ if ( $categories ) {
110
+
111
+ echo '<ul>';
112
+
113
+ foreach ( $categories as $category ) {
114
+
115
+ echo '<li class="cat1"'.$category->slug.'"><a href="'.esc_url(get_category_link($category->term_id)).'">'.$category->name.'</a></li>';
116
+
117
+ }
118
+
119
+ echo '</ul>';
120
+
121
+ }
226
122
 
227
123
  ?>
228
124
 
229
- <div class="newarrival_nodata">
230
-
231
- <p>記事がありません</p>
232
-
233
- </div>
234
-
235
-
236
-
237
- <?php
238
-
239
- endif;
240
-
241
- wp_reset_postdata();
242
-
243
- ?>
244
-
245
-             <?php $prevpost = get_adjacent_post(false, '', true); if ($prevpost) : ?>
246
-
247
- <ul class="newarrival_list">
248
-
249
- <?php
250
-
251
- //記事データを一つづつ表示処理
252
-
253
- foreach($posts_array as $post):
254
-
255
- setup_postdata($posts_array);
256
-
257
- ?>
258
-
259
- <li class="custom"><a href="<?php echo get_permalink($prevpost->ID); ?>">
260
-
261
- <div class="img-wrap">
262
-
263
- <!--画像を追加-->
264
-
265
- <?php
266
-
267
- // アイキャッチ画像の確認
268
-
269
- if ( has_post_thumbnail()) {
270
-
271
- // 存在する
272
-
273
- the_post_thumbnail($prevpost->ID);
274
-
275
- }
276
-
277
- else {
278
-
279
- // 存在しない
280
-
281
- echo '<img src="noimage.jpg">';
282
-
283
- }
284
-
285
- ?></div>
286
-
287
- <!--リンククラスつきのタイトルを追加-->
288
-
289
- <div class="img-wrap2">
290
-
291
- <h2><?php the_title($prevpost->post_title); ?></h2></div>
292
-
293
- <!--日付けを追加-->
294
-
295
-   <div class="img-wrap3">
296
-
297
- <?php the_time('Y/m/d', $prevpost->ID); ?></div>
298
-
299
- <!--リンククラス付きのカテゴリーを追加-->
300
-
301
- <?php
302
-
303
- $categories = get_the_category($prevpost->ID);
304
-
305
- if ( $categories ) {
306
-
307
- echo '<ul>';
308
-
309
- foreach ( $categories as $category ) {
310
-
311
- echo '<li class="cat1"'.$category->slug.'"><a href="'.esc_url(get_category_link($category->term_id)).'">'.$category->name.'</a></li>';
312
-
313
- }
314
-
315
- echo '</ul>';
316
-
317
- }
318
-
319
- ?>
320
-
321
-   <!--リンククラス付きのコメント数を追加-->
322
-
323
- <?php $num_comments = get_comments_number($prevpost->ID);
324
-
325
- if ( $num_comments == 0 ) {
326
-
327
- $comments = __('No Comments'); // 댓글이 없을 경우
328
-
329
- } elseif ( $num_comments > 1 ) {
330
-
331
- $comments = $num_comments . __(' Comments'); // 댓글이 2개 이상일 경우
332
-
333
- } else {
334
-
335
- $comments = __('1 Comment'); // 댓글이 1개일 경우
336
-
337
- }
338
-
339
- $write_comments = '<span class="singlecomments"><a href="' . get_comments_link() .'">'. $comments.'</a></span>';
340
-
341
- echo $write_comments;
342
-
343
- ?>
344
-
345
- <!--リンククラス付きのコメントの抜粋を追加-->
346
-
347
- <div class="img-wrap4">
348
-
349
- <?php the_excerpt($prevpost->ID); ?></div>
350
-
351
- <!--リンククラス付きの記事を読むを追加-->
352
-
353
- <div class="img-wrap5">
354
-
355
- <a href="<?php the_permalink($prevpost->ID); ?>">記事を読む</a></div>
356
-
357
- <?php endforeach; ?>
358
-
359
- </ul>
360
-
361
-
362
-
363
- <?php
364
-
365
- //記事データがない場合の処理
366
-
367
- else:
125
+   <!--リンククラス付きのコメント数を追加-->
126
+
127
+ <?php $num_comments = get_comments_number();
128
+
129
+ if ( $num_comments == 0 ) {
130
+
131
+ $comments = __('No Comments'); // 댓글이 없을 경우
132
+
133
+ } elseif ( $num_comments > 1 ) {
134
+
135
+ $comments = $num_comments . __(' Comments'); // 댓글이 2개 이상일 경우
136
+
137
+ } else {
138
+
139
+ $comments = __('1 Comment'); // 댓글이 1개일 경우
140
+
141
+ }
142
+
143
+ $write_comments = '<span class="singlecomments"><a href="' . get_comments_link() .'">'. $comments.'</a></span>';
144
+
145
+ echo $write_comments;
368
146
 
369
147
  ?>
370
148
 
371
- <div class="newarrival_nodata">
372
-
373
- <p>記事がありません</p>
374
-
375
- </div>
376
-
377
-
378
-
379
- <?php
380
-
381
- endif;
382
-
383
- wp_reset_postdata();
384
-
385
- ?>
386
-
387
-
388
-
389
- </main>
390
-
391
-
392
-
393
- </ul>
394
-
395
- </div>
396
-
397
-
398
-
399
- <div class="pnavi">
400
-
401
- <?php //ページリスト表示処理
402
-
403
- global $wp_rewrite;
404
-
405
- $paginate_base = get_pagenum_link(1);
406
-
407
- if(strpos($paginate_base, '?') || !$wp_rewrite->using_permalinks()){
408
-
409
- $paginate_format = '';
410
-
411
- $paginate_base = add_query_arg('paged','%#%');
412
-
413
- }else{
414
-
415
- $paginate_format = (substr($paginate_base,-1,1) == '/' ? '' : '/') .
416
-
417
- user_trailingslashit('page/%#%/','paged');
418
-
419
- $paginate_base .= '%_%';
420
-
421
- }
422
-
423
- echo paginate_links(array(
424
-
425
- 'base' => $paginate_base,
426
-
427
- 'format' => $paginate_format,
428
-
429
- 'total' => $the_query->max_num_pages,
430
-
431
- 'mid_size' => 1,
432
-
433
- 'current' => ($paged ? $paged : 1),
434
-
435
- 'prev_text' => '< 前へ',
436
-
437
- 'next_text' => '次へ >',
438
-
439
- ));
440
-
441
- ?>
442
-
443
- </div>
444
-
445
- </div>
149
+ <!--リンククラス付きのコメントの抜粋を追加-->
150
+
151
+ <div class="img-wrap4">
152
+
153
+ <?php the_excerpt(); ?></div>
154
+
155
+ <!--リンククラス付きの記事を読むを追加-->
156
+
157
+ <div class="img-wrap5">
158
+
159
+ <a href="<?php the_permalink(); ?>">記事を読む</a></div>
160
+
161
+ </ul>
162
+
163
+
164
+
165
+ <?php
166
+
167
+ endwhile;
168
+
169
+ else:
170
+
171
+ ?>
172
+
173
+ <p>記事はありません!</p>
174
+
175
+
176
+
177
+ <?php
178
+
179
+ endif;
180
+
181
+ ?><!-- ループ終了 -->
182
+
183
+
184
+
185
+ <div class="pager">
186
+
187
+ <?php global $wp_rewrite; $paginate_base = get_pagenum_link(1); if(strpos($paginate_base, '?') || ! $wp_rewrite->using_permalinks()){
188
+
189
+ $paginate_format = '';
190
+
191
+ $paginate_base = add_query_arg('paged','%#%');
192
+
193
+ }
194
+
195
+ else{
196
+
197
+ $paginate_format = (substr($paginate_base,-1,1) == '/' ? '' : '/') .
198
+
199
+ user_trailingslashit('page/%#%/','paged');;
200
+
201
+ $paginate_base .= '%_%';
202
+
203
+ }
204
+
205
+ echo paginate_links(array(
206
+
207
+ 'base' => $paginate_base,
208
+
209
+ 'format' => $paginate_format,
210
+
211
+ 'total' => $wp_query->max_num_pages,
212
+
213
+ 'mid_size' => 5,
214
+
215
+ 'current' => ($paged ? $paged : 1),
216
+
217
+ 'prev_text' => '«',
218
+
219
+ 'next_text' => '»',
220
+
221
+ )); ?></div>
222
+
223
+
446
224
 
447
225
 
448
226
 
449
227
  <?php get_footer(); ?>
450
228
 
451
-
229
+ ```
452
230
 
453
231
 
454
232
 
@@ -456,50 +234,6 @@
456
234
 
457
235
 
458
236
 
459
- <?php
460
-
461
- //記事データがない場合の処理
462
-
463
- else:
464
-
465
- ?>
466
-
467
- <div class="newarrival_nodata">
468
-
469
- <p>記事がありません</p>
470
-
471
- </div>
472
-
473
-
474
-
475
- <?php
476
-
477
- endif;
478
-
479
- wp_reset_postdata();
480
-
481
- ?>
482
-
483
- 削除して
484
-
485
- <?php
486
-
487
- endwhile;
488
-
489
-
490
-
491
- $html_top_result .= '</div>';
237
+ $paged = get_query_var('page');
492
-
493
-
494
-
495
- else :
238
+
496
-
497
- $html_top_result = '<div><p>ありません。</p></div>';
239
+ ダッシュボードの表示数と'posts_per_page'の取得数を揃える
498
-
499
- endif;
500
-
501
-
502
-
503
- echo $html_top_result;
504
-
505
- ?>に書き換え