ページ送りの関数作成で質問させてください。
-
prev_next_link();
と言うページ送りの関数を作ってみたのですが、理解できない部分があったので、質問させてください。single.phpのループ内endwhile直前に
<?php echo prev_next_link(); ?>
として、
functions.phpに以下の関数を作成してページ送りをAjaxな半自作テーマで実現しています。function prev_next_link(){ $currentId = get_the_ID(); $posts = get_posts( 'order=desc&orderby=date&numberposts=-1' ); $c = 0; echo '<div class="nav-below">'; foreach( $posts as $post ) : setup_postdata( $post ); $postid[] = get_the_ID(); if ( $postid[ $c ] == $currentId ) { if ( $c > 0 ) { $prevC = $c - 1; $prevId = $postid[ $prevC ]; $prevLink = get_permalink( $prevId ); $prevTitle = get_the_title( $prevId ); echo '<span class="nav-previous"><a href="'.$prevLink.'#'.$prevId.'" title="'.$prevTitle.'">'.$prevTitle.'</a></span>'; } $nextC = $c + 1; } elseif ( $c == $nextC && $nextC < count( $posts ) ) { $nextId = $postid[ $nextC ]; $nextLink = get_permalink( $nextId ); $nextTitle = get_the_title( $nextId ); } ++$c; endforeach; if ( $nextC < count( $posts ) ) { echo '<span class="nav-next"><a href="'.$nextLink.'#'.$nextId.'" title="'.$nextTitle.'">'.$nextTitle.'</a></span>'; } echo '</div>'; }
上記は一応機能していますが、以下の様にnextのリンクをforeachの中に記述すると、nextが2つ、表示中の記事のリンクと次ページのリンクが表示されてしまいます。
function prev_next_link(){ $currentId = get_the_ID(); $posts = get_posts( 'order=desc&orderby=date&numberposts=-1' ); $c = 0; echo '<div class="nav-below">'; foreach( $posts as $post ) : setup_postdata( $post ); $postid[] = get_the_ID(); if ( $postid[ $c ] == $currentId ) { if ( $c > 0 ) { $prevC = $c - 1; $prevId = $postid[ $prevC ]; $prevLink = get_permalink( $prevId ); $prevTitle = get_the_title( $prevId ); echo '<span class="nav-previous"><a href="'.$prevLink.'#'.$prevId.'" title="'.$prevTitle.'">'.$prevTitle.'</a></span>'; } $nextC = $c + 1; } elseif ( $c == $nextC && $nextC < count( $posts ) ) { $nextId = $postid[ $nextC ]; $nextLink = get_permalink( $nextId ); $nextTitle = get_the_title( $nextId ); echo '<span class="nav-next"><a href="'.$nextLink.'#'.$nextId.'" title="'.$nextTitle.'">'.$nextTitle.'</a></span>'; } ++$c; endforeach; echo '</div>'; }
どういう訳でそうなるのか教えて頂けないでしょうか、また改善策もご教授いただければ幸いです。
宜しくお願い致します。
3件の返信を表示中 - 1 - 3件目 (全3件中)
3件の返信を表示中 - 1 - 3件目 (全3件中)
- トピック「ページ送りの関数作成で質問させてください。」には新たに返信することはできません。