WordPressの一覧表示をトップページとそれ以外で変更する
WordPressの一覧表示をトップページとそれ以外で変更する方法をメモしておきます。利用しているWordPressのバージョン、および、テンプレートによって違ってくる部分も大いにあるかと思います。環境と変更内容、方針は以下の通りです。
環境
環境は以下の通りです。
| ソフトウェア | バージョン | 
|---|---|
| WordPress | 3.5.1 | 
| Themify (WordPress テーマ) | 1.2.9 | 
| Themify Basic | 1.1.0 | 
変更内容
| 対象 | 表示内容 | 
|---|---|
| トップページ(ホームページ)、検索結果 | 記事タイトルと概要を5件表示 | 
| その他のページ(カテゴリ・タグ・日付別) | 記事タイトルを20件表示 | 
変更方針
- トップページ以外は表示設定で制御する
 - トップページの件数表示はテンプレートに手を加えて制御する
 
表示件数の制御
トップページ以外の表示設定
管理 > 設定 > 表示設定 の 1ページに表示する最大投稿数 を20件に変更します。
トップページの表示設定
テーマディレクトリの functions.phpに以下を追記します。
33 34 35 36 37 38 39 40  | add_action('pre_get_posts', 'my_pre_get_posts'); function my_pre_get_posts($query) {  if ($query->is_main_query() && !is_admin()) {  if (is_home() || is_front_page() || is_search()) {  $query->set('posts_per_page', 5);  }  } }  | 
以上で、表示件数の制御は完了です。
 上記の設定に加えて、特定のカテゴリ(例えば、カテゴリID 5 であるもの)は、10件表示にしたいと言う場合は、以下のように行えば良いです。
35 36 37 38 39 40 41  | // 〜省略  if (is_home() || is_front_page() || is_search()) {  $query->set('posts_per_page', 5);  } else if (is_category(5)) {  $query->set('posts_per_page', 10);  } // 〜省略  | 
表示件数の制御
カテゴリ・タグ・日付別ページで表示内容を変える
色々なやり方があるとおもいますが、凝ったことをやりたい訳ではないので、手っ取り早く、まとめて変更したいと思います。既存のものを活かしつつ、少し手を加えるだけにします。
 カテゴリ・タグ・日付別ページで使うテンプレートとして、 includes/loop.phpを複製し、 includes/loop-title.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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61  | <?php if(!is_single()){ global $more; $more = 0; } //enable more link ?> <?php  /** Themify Default Variables  *  @var object */ global $themify; ?> <?php themify_post_before(); // hook ?> <article id="post-<?php the_ID(); ?>" <?php post_class("post clearfix post-title-list " . $themify->get_categories_as_classes(get_the_ID())); ?>>  <?php themify_post_start(); // hook ?>  <div class="post-content">  <?php if($themify->hide_title != 'yes'): ?>  <?php themify_before_post_title(); // Hook ?>  <?php if($themify->unlink_title == 'yes'): ?>  <h1 class="post-title"><?php the_title(); ?></h1>  <?php else: ?>  <h1 class="post-title"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h1>  <?php endif; //unlink post title ?>  <?php themify_after_post_title(); // Hook ?>   <?php endif; //post title ?>      <?php if($themify->hide_meta != 'yes'): ?>  <p class="post-meta">   <?php if($themify->hide_date != 'yes'): ?>  <time datetime="<?php the_time('o-m-d') ?>" class="post-date" pubdate><?php the_time(apply_filters('themify_loop_date', 'Y年n月j日')) ?></time>  <?php endif; //post date ?>  <span class="post-category"><?php the_category(', ') ?></span>  <?php the_tags(' <span class="post-tag">', ', ', '</span>'); ?>  <?php  if( !themify_get('setting-comments_posts') && comments_open() ) : ?>  <span class="post-comment"><?php comments_popup_link( __( '0 Comment', 'themify' ), __( '1 Comment', 'themify' ), __( '% Comments', 'themify' ) ); ?></span>  <?php endif; //post comment ?>  </p>  <?php endif; //post meta ?>      <?php if($themify->display_content == 'excerpt'): ?>  <?php the_excerpt(); ?>  <?php elseif($themify->display_content == 'none'): ?>  <?php else: ?>  <?php endif; //display content ?>  <?php edit_post_link(__('Edit', 'themify'), '[', ']'); ?>  </div>  <!-- /.post-content -->  <?php themify_post_end(); // hook ?> </article> <?php themify_post_after(); // hook ?> <!-- /.post -->  | 
最後に、 index.phpに、カテゴリ・タグ・日付別が呼ばれた場合の分岐を追加します。
79 80 81 82 83 84 85 86 87 88 89  |  <?php while (have_posts()) : the_post(); ?>  <?php if(is_search()): ?>  <?php get_template_part( 'includes/loop' , 'search'); ?>  <?php elseif(is_category() || is_tag() || is_date()): ?>  <?php get_template_part( 'includes/loop-title' , 'index'); ?>  <?php else: ?>  <?php get_template_part( 'includes/loop' , 'index'); ?>  <?php endif; ?>  <?php endwhile; ?>  | 
以上で完了です。
 category.php, tag.php, date.php を用いる方法でも良いとおもいます。(Themifyは、それらのテンプレートは存在しないため、新規作成する必要があります)