• 解決済 eijiy

    (@eijiy)


    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件中)
  • $nextC を明示的に初期化していないので PHP的には 0 が入っていると思われます。
    で、現在の記事が最初の記事じゃない場合 foreach の1回目のループで
    ( $c == $nextC && $nextC < count( $posts ) ) が成立して
    そこで「表示中の記事のリンク」が表示されます。

    改善策:
    $c = 0;
    に続けて
    $nextC = -1;
    としてみるとか。

    トピック投稿者 eijiy

    (@eijiy)

    kzさん、返信ありがとうございます。

    ご教授頂いたように、$nextC = -1;で解決しました。またelseif ( $c == $nextC && $nextC < count( $posts ) ) {'の$c == $nextC$c === $nextC`としても、どちらでも解決しました。

    有難うございました。

    function prev_next_link(){
    
            $currentId = get_the_ID();
    
    		$posts = get_posts( 'order=desc&orderby=date&numberposts=-1' );
    
    		$c = 0; $nextC = -1;
    
    		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件中)
  • トピック「ページ送りの関数作成で質問させてください。」には新たに返信することはできません。