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

質問編集履歴

1

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

2019/02/09 06:39

投稿

_yamamo
_yamamo

スコア10

title CHANGED
File without changes
body CHANGED
@@ -27,7 +27,8 @@
27
27
  ```
28
28
 
29
29
  # 各ループの処理
30
- 個別に読み込めば正しく表示されることは確認済みです
30
+ 個別に読み込めば正しく表示されることは確認済みです
31
+ 二つを同一ページに読み込むと、後から読み込んだ方が表示されません。
31
32
 
32
33
  ### アンカーリンク
33
34
  ```
@@ -59,7 +60,7 @@
59
60
  wp_reset_postdata();
60
61
  }
61
62
  $thisyear01 = date('Y');
62
- for ($year01=$thisyear01; $year01 >= 2000; $year01--) {
63
+ for ($year01=$thisyear01; $year01 >= 2017; $year01--) {
63
64
  archiveFunc($year01);
64
65
  }
65
66
  ?>
@@ -95,14 +96,80 @@
95
96
  wp_reset_postdata();
96
97
  }
97
98
  $thisyear02 = date('Y');
98
- for ($year02=$thisyear02; $year02 >= 2000; $year02--) {
99
+ for ($year02=$thisyear02; $year02 >= 2017; $year02--) {
99
100
  archiveFunc($year02);
100
101
  }
101
102
  ?>
102
103
  ```
103
104
 
105
+ ### 試してみた方法
106
+ ```
107
+ <!-- アンカーリンク -->
108
+ <?php
109
+ function archiveFunc($year){
110
+ $newslist = new WP_Query( array(
111
+ 'ignore_sticky_posts' => true,
112
+ 'category_name' => 'dietician',
113
+ 'posts_per_page' => -1,
114
+ 'year' => $year
115
+ ));
116
+ if($newslist->have_posts()) :
117
+ ?>
118
+ <section class="h3_box">
119
+ <ul class="anc" style="margin-bottom: 10px;">
120
+ <li><a href="dietician_detail.html#year-<?php echo $year; ?>"><?php echo $year; ?>年の記事見出し</a></li>
121
+ </ul>
122
+ <ul class="bullet li-half">
123
+ <?php while($newslist->have_posts()) : $newslist->the_post(); ?>
124
+ <li>&nbsp;&nbsp;<a href="dietician_detail.html#post-<?php the_ID(); ?>"><?php the_title(); ?></a></li>
125
+ <?php endwhile; ?>
126
+ </ul>
127
+ </section>
128
+
129
+ <!-- 記事一覧 -->
130
+ <section class="h3_box">
131
+ <h2 id="year-<?php echo $year; ?>"><?php echo $year; ?>年の記事見出し</h2>
132
+
133
+ <?php while($newslist->have_posts()) : $newslist->the_post(); ?>
134
+
135
+ <h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>
136
+ <div class="image_border"><?php the_content(); ?></div>
137
+
138
+ <?php endwhile; ?>
139
+ </section>
140
+
141
+ <?php endif;
142
+ wp_reset_postdata();
143
+ }
144
+ $thisyear = date('Y');
145
+ for ($year=$thisyear; $year >= 2017; $year--) {
146
+ archiveFunc($year);
147
+ }
148
+ ?>
149
+ ```
150
+ 上記の通り修正したところ2つの処理は正常にされているのですが
151
+ ```
152
+ ・2018年の記事見出し(見出しへのアンカーリンク)
153
+  2018年の記事1(記事へのアンカーリンク)
154
+  2018年の記事2(記事へのアンカーリンク)
155
+
156
+ ・2018年の記事見出し
157
+  2018年の記事内容1
158
+  2018年の記事内容2
159
+
160
+ ・2017年の記事見出し(見出しへのアンカーリンク)
161
+  2017年の記事1(記事へのアンカーリンク)
162
+  2017年の記事2(記事へのアンカーリンク)
163
+
164
+ ・2017年の記事見出し
165
+  2017年の記事内容1
166
+  2017年の記事内容2
167
+ ```
168
+ となってしまい、想定通りの表示になりませんでした。
169
+
104
- 最初はそれぞれquery_postsを使用したメインループで書いていたのですが
170
+ また、最初はそれぞれquery_postsを使用したメインループで書いていたのですが
105
- 調べたところ複数使えいとことなので、WP_Queryを使用したサブループに書き換えました。
171
+ 調べたところ非推奨だったようなので、WP_Queryを使用したサブループに書き換えました。
172
+
106
173
  その他、気を付ける点や書き換えるべき点はありますでしょうか。
107
174
  WordPressはまだまだ勉強中のため、ご教示いただけますと幸いです。
108
175
  どうぞよろしくお願いいたします。