質問編集履歴

1

試してみた修正内容を追記しました。

2019/02/09 06:39

投稿

_yamamo
_yamamo

スコア10

test CHANGED
File without changes
test CHANGED
@@ -56,7 +56,9 @@
56
56
 
57
57
  # 各ループの処理
58
58
 
59
- 個別に読み込めば正しく表示されることは確認済みです
59
+ 個別に読み込めば正しく表示されることは確認済みです
60
+
61
+ 二つを同一ページに読み込むと、後から読み込んだ方が表示されません。
60
62
 
61
63
 
62
64
 
@@ -120,7 +122,7 @@
120
122
 
121
123
  $thisyear01 = date('Y');
122
124
 
123
- for ($year01=$thisyear01; $year01 >= 2000; $year01--) {
125
+ for ($year01=$thisyear01; $year01 >= 2017; $year01--) {
124
126
 
125
127
  archiveFunc($year01);
126
128
 
@@ -192,7 +194,7 @@
192
194
 
193
195
  $thisyear02 = date('Y');
194
196
 
195
- for ($year02=$thisyear02; $year02 >= 2000; $year02--) {
197
+ for ($year02=$thisyear02; $year02 >= 2017; $year02--) {
196
198
 
197
199
  archiveFunc($year02);
198
200
 
@@ -204,9 +206,141 @@
204
206
 
205
207
 
206
208
 
209
+ ### 試してみた方法
210
+
211
+ ```
212
+
213
+ <!-- アンカーリンク -->
214
+
215
+ <?php
216
+
217
+ function archiveFunc($year){
218
+
219
+ $newslist = new WP_Query( array(
220
+
221
+ 'ignore_sticky_posts' => true,
222
+
223
+ 'category_name' => 'dietician',
224
+
225
+ 'posts_per_page' => -1,
226
+
227
+ 'year' => $year
228
+
229
+ ));
230
+
231
+ if($newslist->have_posts()) :
232
+
233
+ ?>
234
+
235
+ <section class="h3_box">
236
+
237
+ <ul class="anc" style="margin-bottom: 10px;">
238
+
239
+ <li><a href="dietician_detail.html#year-<?php echo $year; ?>"><?php echo $year; ?>年の記事見出し</a></li>
240
+
241
+ </ul>
242
+
243
+ <ul class="bullet li-half">
244
+
245
+ <?php while($newslist->have_posts()) : $newslist->the_post(); ?>
246
+
247
+ <li>&nbsp;&nbsp;<a href="dietician_detail.html#post-<?php the_ID(); ?>"><?php the_title(); ?></a></li>
248
+
249
+ <?php endwhile; ?>
250
+
251
+ </ul>
252
+
253
+ </section>
254
+
255
+
256
+
257
+ <!-- 記事一覧 -->
258
+
259
+ <section class="h3_box">
260
+
261
+ <h2 id="year-<?php echo $year; ?>"><?php echo $year; ?>年の記事見出し</h2>
262
+
263
+
264
+
265
+ <?php while($newslist->have_posts()) : $newslist->the_post(); ?>
266
+
267
+
268
+
269
+ <h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>
270
+
271
+ <div class="image_border"><?php the_content(); ?></div>
272
+
273
+
274
+
275
+ <?php endwhile; ?>
276
+
277
+ </section>
278
+
279
+
280
+
281
+ <?php endif;
282
+
283
+ wp_reset_postdata();
284
+
285
+ }
286
+
287
+ $thisyear = date('Y');
288
+
289
+ for ($year=$thisyear; $year >= 2017; $year--) {
290
+
291
+ archiveFunc($year);
292
+
293
+ }
294
+
295
+ ?>
296
+
297
+ ```
298
+
299
+ 上記の通り修正したところ2つの処理は正常にされているのですが
300
+
301
+ ```
302
+
303
+ ・2018年の記事見出し(見出しへのアンカーリンク)
304
+
305
+  2018年の記事1(記事へのアンカーリンク)
306
+
307
+  2018年の記事2(記事へのアンカーリンク)
308
+
309
+
310
+
311
+ ・2018年の記事見出し
312
+
313
+  2018年の記事内容1
314
+
315
+  2018年の記事内容2
316
+
317
+
318
+
319
+ ・2017年の記事見出し(見出しへのアンカーリンク)
320
+
321
+  2017年の記事1(記事へのアンカーリンク)
322
+
323
+  2017年の記事2(記事へのアンカーリンク)
324
+
325
+
326
+
327
+ ・2017年の記事見出し
328
+
329
+  2017年の記事内容1
330
+
331
+  2017年の記事内容2
332
+
333
+ ```
334
+
335
+ となってしまい、想定通りの表示になりませんでした。
336
+
337
+
338
+
207
- 最初はそれぞれquery_postsを使用したメインループで書いていたのですが
339
+ また、最初はそれぞれquery_postsを使用したメインループで書いていたのですが
208
-
340
+
209
- 調べたところ複数使えいとことなので、WP_Queryを使用したサブループに書き換えました。
341
+ 調べたところ非推奨だったようなので、WP_Queryを使用したサブループに書き換えました。
342
+
343
+
210
344
 
211
345
  その他、気を付ける点や書き換えるべき点はありますでしょうか。
212
346