have_posts()でカテゴリ分岐


//カテゴリーID 2の記事
 if( have_posts() ) {
		 while(have_posts() ) {
		 the_post();
			 $cat = get_the_category(); //現在の記事のカテゴリー情報を取得
			 	if( $cat[0]->term_id===1) //現在の記事のカテゴリーIDを取得
			 get_template_part( 'content');
		 }
 } 
//カテゴリーID 2の記事
  if( have_posts() ) {
		 while(have_posts() ) {
		 the_post();
			 	 $cat = get_the_category(); 
			 	if( $cat[0]->term_id===2) 
			 get_template_part( 'content');
		 }
 } 

query_posts、get_posts を使うとページャーが動かなくなるので注意!


Categories:

Tags: