指定カテゴリーの投稿一覧(ページ切替機能付)作成
-
関数リファレンスの「query_posts関数を使って指定カテゴリーの投稿一覧(ページ切替機能付)を表示します」を真似して、get_postsを使ってfunctions.phpでやってみましたが、記事がランダムに表示されています。
function prev_next_posts_link(){ $cat = get_query_var( 'cat' ); $post_count = 5; $posts = get_posts("category=$cat&numberposts=-1&offset=0&orderby=date&order=desc"); $post_max = count( $posts ); $pid = intval( $_GET['pid'] ); if( $pid < 1 ){ $pid = 1; $offset = 0; }else{ $pid = intval( $_GET['pid'] ); $offset = ( $pid - 1 ) * $post_count; } $posts = get_posts("category=$cat&numberposts=$post_count&offset=$offset&orderby=date&order=desc"); foreach( $posts as $post ) : setup_postdata( $post ); echo '<div class="post"><div class="time">'; the_time('Y'); echo '<br />'; the_time('m/d'); echo '</div><div id="titleinfo"><h2 class="title"><a href="'; the_permalink(); echo '#'; the_ID(); echo '" title="'; the_title(); echo '">'; the_title(); echo '</a></h2><span>'; echo comments_number( 'no comment', '1 coment', '% comments' ); echo '</span></div><br class="clear" /><p>'; echo mb_substr(strip_tags( $post->post_content ), 0, 100 ); echo '<a href="'; the_permalink(); echo '#'; echo get_the_ID(); echo '" title="'; the_title(); echo '" class="more-link"> 続きを読む</a></p></div>'; endforeach; get_posts( "" ); if( $pid > 1 ){ $page = $pid - 1; echo '<a href="./?pid='.attribute_escape( $page ).'">新しい記事</a>'; } if( ($offset + $post_count) < $post_max){ $page = $pid + 1; echo '<a href="./?pid='.attribute_escape( $page ).'">過去の記事</a>'; } }
get_posts();
では無理なのか、それともループ外では出来ないのか、また上記コードの$pid = intval( $_GET['pid'] ); if( $pid < 1 ){ $pid = 1; $offset = 0; }else{ $pid = intval( $_GET['pid'] ); $offset = ( $pid - 1 ) * $post_count; }
この部分はページ送りと関係があるのでしょうか。
やりたい事はAjax非同期通信で、選択されたカテゴリーの投稿一覧(抜粋表示)を5件ずつページ送りしたいと言う事で、自分のAjaxの仕様上、urlの末尾に「#とページや記事、カテゴリーのID」を追加したいので、WordPressの簡単なタグでは無理なのかなと自信は無いながらも思っています。
上記の質問にどなたかお答え頂けないでしょうか。
宜しくお願い致します。
2件の返信を表示中 - 1 - 2件目 (全2件中)
2件の返信を表示中 - 1 - 2件目 (全2件中)
- トピック「指定カテゴリーの投稿一覧(ページ切替機能付)作成」には新たに返信することはできません。