0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

WordPressのメインループ

Posted at

メインループ

WordPressではパラメーターからのリクエストに応じてデータベースの検索が行われて、該当する投稿データが返される。この時、データベースを検索する条件を「メインクエリ」といい、返ってきた投稿データをページに出力するための処理を「メインループ」と呼びます。

メインループの制御方法:アクションフック:pre_get_posts

pre_get_postsは、メインクエリを解析後、データベースから投稿データを検索する前に実行されるアクションフックです。 WordPressの規定の処理中のイベントに任意の処理を追加する仕組みであり、追加する処理の内容はfunctions.phpに記述します。

function custom_loop_rules ( $query ) {
    // 管理画面や、メインクエリ以外の処理に影響を及ぼさないように。pre_get_postsの設定は管理画面上の処理やメインクエリ以外の処理にも影響するので、特段の事情がない限りは、以下のように操作対象から除外しておきます。
    if ( is_admin() || ! $query->is_main_query() ){
        return;
    }
//フロントページの投稿数は、3にする。
if ( is_front_page() ) {
    $query->set( 'posts_per_page', 3 );
	  }
// カスタム投稿タイプ「movie」のみの場合
if(is_post_type_archive( 'movie' )){
$query->set('posts_per_page',12);
$query->set('order','DESC')
//配列は使えない。
}
}
add_action('pre_get_posts','custom_loop_rules');

上記のif分のところに、追加をしていく。 条件分岐は WordPressCodex を確認する。

pre _get_postsの使い方

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?