目次
		  
		  
		  
		
色々要望があって、試みていくうちにループが思うようにできなくなった
なんてことありますよね。私も昔はよく陥っていました。
archive.phpやcotegory.phpで、複雑なコードを書いてしまって、他のカテゴリが取得されてしまったり、ページ送りができなくなってしまうことがあります。
難しく考えすぎて動かなくなってしまう事が多いので、まず下記のコードをarchive.phpやcotegory.phpにコピペして下さい。
まずテンプレートにループの記述
archive.php cotegory.php
| 
					 1 2 3 4 5 6 7  | 
						<?php if (have_posts()) : ?>   <?php while (have_posts()) : the_post(); ?>     ここにループさせるコード   <?php endwhile; ?>   <?php if(function_exists('wp_pagenavi')) : wp_pagenavi(); endif; ?><!--WP-PageNavi--> <?php endif; ?> <?php wp_reset_query(); ?>  | 
					
pre_get_postsでのループ
ただこれだけだと表示条件等の設定が書いてありません。
この設定はfunctions.phpに記載します。
ループのオプション設定を記述
functions.php
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30  | 
						function news_posts_per_page($query) {     if ( is_admin() || ! $query->is_main_query() )     return;     if ( $query->is_category( 'ここにカテゴリ名' ) ) {             /* 表示件数 */             $query->set( 'posts_per_page', '10' );             /* 並び順条件 */             $query->set('orderby','post_date');             /* 降順昇順の設定 */             $query->set('order', 'DESC');             /* カテゴリーID指定 */             $query->set( 'cat','5' );             /* カテゴリーID除外 */             $query->set( 'cat','-3' );             /* カテゴリースラッグ指定 */             $query->set( 'category_name','ここにスラッグ' );             /* 特定カテゴリー除外 */             $query->set( 'category__not_in',array(4,5) );             return;     } } add_action( 'pre_get_posts', 'news_posts_per_page' );  | 
					
カスタム投稿タイプの条件設定する場合
投稿カテゴリーは上記設定にして、カスタム投稿タイプの条件設定する場合は上記のコードを
functions.php
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36  | 
						function news_posts_per_page($query) {     if ( is_admin() || ! $query->is_main_query() )     return;     if ( $query->is_category( 'ここにカテゴリ名' ) ) {             /* 表示件数 */             $query->set( 'posts_per_page', '10' );             /* 並び順条件 */             $query->set('orderby','post_date');             /* 降順昇順の設定 */             $query->set('order', 'DESC');             /* カテゴリーID指定 */             $query->set( 'cat','5' );             /* カテゴリーID除外 */             $query->set( 'cat','-3' );             /* カテゴリースラッグ指定 */             $query->set( 'category_name','ここにスラッグ' );             /* 特定カテゴリー除外 */             $query->set( 'category__not_in',array(4,5) );             return;     }     if ( $query->is_post_type_archive( 'ここにポストタイプスラッグ' ) ) {             $query->set( 'posts_per_page', '10' );             $query->set('orderby','post_date');             $query->set('order', 'DESC');             return;     } } add_action( 'pre_get_posts', 'news_posts_per_page' );  | 
					
これでarchive.phpやcotegory.phpはシンプルにしておくことができます。