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