WordPressの年別アンカーリンクと年毎の記事一覧表示の両立
WordPressにて下記のようなページを作りたいと思っているのですが、
アンカーリンクのループを追加すると個別記事のループが表示されず
個別記事のループを追加するとアンカーリンクのループが表示されなくなってしまい
ページがループ箇所以降読み込まれず、白くなってしまいます。
実装したい内容
--- 以下年別アンカーリンク --- ・2018年の記事見出し(見出しへのアンカーリンク) 2018年の記事1(記事へのアンカーリンク) 2018年の記事2(記事へのアンカーリンク) ・2017年の記事見出し(見出しへのアンカーリンク) 2017年の記事1(記事へのアンカーリンク) 2017年の記事2(記事へのアンカーリンク) --- 以下年別見出しと個別記事の一覧表示 --- ・2018年の記事見出し 2018年の記事内容1 2018年の記事内容2 ・2017年の記事見出し 2017年の記事内容1 2017年の記事内容2
各ループの処理
個別に読み込めば正しく表示されることは確認済みですが
二つを同一ページに読み込むと、後から読み込んだ方が表示されません。
アンカーリンク
<?php function archiveFunc($year01){ $newslist01 = new WP_Query( array( 'ignore_sticky_posts' => true, 'category_name' => 'dietician', 'posts_per_page' => -1, 'year' => $year01 )); if($newslist01->have_posts()) : ?> <section class="h3_box"> <ul class="anc" style="margin-bottom: 10px;"> <li><a href="dietician_detail.html#year01-<?php echo $year01; ?>"><?php echo $year01; ?>年の記事見出し</a></li> </ul> <ul class="bullet li-half"> <?php while($newslist01->have_posts()) : $newslist01->the_post(); ?> <li> <a href="dietician_detail.html#post-<?php the_ID(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> </section> <?php endif; wp_reset_postdata(); } $thisyear01 = date('Y'); for ($year01=$thisyear01; $year01 >= 2017; $year01--) { archiveFunc($year01); } ?>
年別見出しと個別記事の一覧表示
<?php function archiveFunc($year02){ $newslist02 = new WP_Query( array( 'ignore_sticky_posts' => true, 'category_name' => 'dietician', 'posts_per_page' => -1, 'year' => $year02 )); if($newslist02->have_posts()) : ?> <section class="h3_box"> <h2 id="year-<?php echo $year02; ?>"><?php echo $year02; ?>年の記事見出し</h2> <?php while($newslist02->have_posts()) : $newslist02->the_post(); ?> <h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3> <div class="image_border"><?php the_content(); ?></div> <?php endwhile; ?> </section> <?php endif; wp_reset_postdata(); } $thisyear02 = date('Y'); for ($year02=$thisyear02; $year02 >= 2017; $year02--) { archiveFunc($year02); } ?>
試してみた方法
<!-- アンカーリンク --> <?php function archiveFunc($year){ $newslist = new WP_Query( array( 'ignore_sticky_posts' => true, 'category_name' => 'dietician', 'posts_per_page' => -1, 'year' => $year )); if($newslist->have_posts()) : ?> <section class="h3_box"> <ul class="anc" style="margin-bottom: 10px;"> <li><a href="dietician_detail.html#year-<?php echo $year; ?>"><?php echo $year; ?>年の記事見出し</a></li> </ul> <ul class="bullet li-half"> <?php while($newslist->have_posts()) : $newslist->the_post(); ?> <li> <a href="dietician_detail.html#post-<?php the_ID(); ?>"><?php the_title(); ?></a></li> <?php endwhile; ?> </ul> </section> <!-- 記事一覧 --> <section class="h3_box"> <h2 id="year-<?php echo $year; ?>"><?php echo $year; ?>年の記事見出し</h2> <?php while($newslist->have_posts()) : $newslist->the_post(); ?> <h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3> <div class="image_border"><?php the_content(); ?></div> <?php endwhile; ?> </section> <?php endif; wp_reset_postdata(); } $thisyear = date('Y'); for ($year=$thisyear; $year >= 2017; $year--) { archiveFunc($year); } ?>
上記の通り修正したところ2つの処理は正常にされているのですが
・2018年の記事見出し(見出しへのアンカーリンク) 2018年の記事1(記事へのアンカーリンク) 2018年の記事2(記事へのアンカーリンク) ・2018年の記事見出し 2018年の記事内容1 2018年の記事内容2 ・2017年の記事見出し(見出しへのアンカーリンク) 2017年の記事1(記事へのアンカーリンク) 2017年の記事2(記事へのアンカーリンク) ・2017年の記事見出し 2017年の記事内容1 2017年の記事内容2
となってしまい、想定通りの表示になりませんでした。
また、最初はそれぞれquery_postsを使用したメインループで書いていたのですが
調べたところ非推奨だったようなので、WP_Queryを使用したサブループに書き換えました。
その他、気を付ける点や書き換えるべき点はありますでしょうか。
WordPressはまだまだ勉強中のため、ご教示いただけますと幸いです。
どうぞよろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。