サポート » 使い方全般 » 指定カテゴリーの投稿一覧(ページ切替機能付)作成

  • 解決済 eijiy

    (@eijiy)


    関数リファレンスの「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件中)
  • トピック投稿者 eijiy

    (@eijiy)

    カテゴリーのIDが取得出来ていなかったので、直接数字を入れたり以下の様に引数にデータ($data)を渡すと1ページ目は目的の記事が5件表示されて、過去の記事へのリンクも表示される様になりましたが、それをクリックすると、サイドバーのメニューの最新の記事10件の上から5件と同じ記事が5件表示されます。

    関係あるんでしょうか。

    修正箇所
    function prev_next_posts_link( $data ) {

    $posts = get_posts("category=$data&numberposts=-1&offset=0&orderby=date&order=desc");

    $posts = get_posts("category=$data&numberposts=$post_count&offset=$offset&orderby=date&order=desc");

    トピック投稿者 eijiy

    (@eijiy)

    いまだに質問の要領を得ずにご迷惑をおかけして申し訳ありません。

    一応自分なりの解決を得ましたので、有難うございました。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「指定カテゴリーの投稿一覧(ページ切替機能付)作成」には新たに返信することはできません。