サポート » 使い方全般 » AjaxなWPで表示中の単一記事のIDを元にページ送り

  • 解決済 eijiy

    (@eijiy)


    既存のhtmlにワードプレスのタグを挿入したテーマで、Ajax非同期通信を試しています。

    この場合、jQueryで#contents(#mainと#sideを含む)の内容を入れ替えているので、WordPressのget_previous_post();get_next_post();が使えません。

    それで現在表示している単一の記事のIDを元に、まず$posts = get_posts( 'order=desc&orderby=date&numberposts=-1' );で全ての投稿を取得して、前後の投稿日の記事のIDを調べて前や次のページのリンクを出力してみました。

    $c = 0;
    	foreach( $posts as $post ) {
    		setup_postdata( $post );
    		$postid = get_the_ID();
    		if ( $postid == $hash ) {
    			$prevC = $c - 1;
    			$nextC = $c + 1;
    		}
    		++$c;
    	}
    	$c = 0;
    	foreach( $posts as $post ) {
    		setup_postdata( $post );
    		if ( $c == $prevC ) {
    			$prevId = get_the_ID();
    		} elseif ( $c == $nextC ) {
    			$nextId = get_the_ID();
    		}
    		++$c;
    	}
    	$postsC = $c;
    	if ( $prevC > 0 ) {
    		$postPrev = get_posts( 'include='.$prevId );
    		foreach( $postPrev as $post ) {
    			setup_postdata( $post );
    			echo '<a href="';
    			the_permalink();
    			echo '#'.$prevId.'">'; // ajaxでhashを活用
    			$title = get_the_title( $prevId );
    			echo $title.'</a>';
    		}
    	}
    	if ( $nextC < $postsC ) {
    		$postNext = get_posts( 'include='.$nextId );
    		foreach( $postNext as $post ) {
    			setup_postdata( $post );
    			echo '<a href="';
    			the_permalink();
    			echo '#'.$nextId.'">'; // ajaxでhashを活用
    			$title = get_the_title( $nextId );
    			echo $title.'</a>';
    		}
    	}

    しかし次ページのリンクが、5つ同じものが表示されて改善策が見い出せません。

    現状はこんな感じで、単一の記事で確認できます。

    あと一歩だと思うので、どなたかご教授頂けないでしょうか。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • get_previous_post() や get_next_post() の出力も ajax で取ってくれば OK
    ってことではない??

    トピック投稿者 eijiy

    (@eijiy)

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

    いいえ、それでは表示されません。

    single.php

    <div class="nav-below">
    	<span class="nav-previous"><?php previous_post_link('%link', '古い記事へ'); ?></span>
            <span class="nav-next"><?php next_post_link('%link', '新しい記事へ'); ?></span>
    </div><!-- /.nav-below -->

    上記の様になっていますが、クリックされたアンカーリンクに含まれるhash(#記事のID)をjQueryからpage-test.phpに渡して、そこで#contentsの内容を生成してjQueryで表示させているので、自力でページ送りを作成するしかないと思っています。

    間違っていたらご指摘ください。

    宜しくお願い致します。

    トピック投稿者 eijiy

    (@eijiy)

    ページ送りの部分をif文で囲ったら何故か問題は修正されました。

    if ( count($singlepost) == 1 ) {
    		echo '<div class="nav-below">';
    		if ( $prevC > 0 ) {
    			$postPrev = get_posts( 'include='.$prevId );
    			foreach( $postPrev as $post ) {
    				setup_postdata( $post );
    				echo '<span class="nav-previous"><a href="';
    				the_permalink();
    				echo '#'.$prevId.'">';
    				$title = get_the_title( $prevId );
    				echo $title.'</a></span>';
    			}
    		}
    		if ( $nextC < $postsC ) {
    			$postNext = get_posts( 'include='.$nextId );
    			foreach( $postNext as $post ) {
    				setup_postdata( $post );
    				echo '<span class="nav-next"><a href="';
    				the_permalink();
    				echo '#'.$nextId.'" class="nav-next">';
    				$title = get_the_title( $nextId );
    				echo $title.'</a></span>';
    			}
    		}
    		echo '</div>';
    	}

    まだまだAjax化への問題は山積みですが、この件は訳の分らぬうちに自己解決しました。

    有難うございました。

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「AjaxなWPで表示中の単一記事のIDを元にページ送り」には新たに返信することはできません。