LoginSignup
15
16

More than 5 years have passed since last update.

WordPress の面倒くさいページネーションの動作テスト

Last updated at Posted at 2016-10-17

WordPress をインストールしたてだと投稿が一個しかなくて、ページネーションの動作テストが面倒くさい。
わざわざいっぱい投稿して記事数を増やすのも面倒くさい。
Duplicate Post をインストールしてコピーして記事数を増やすのも面倒くさい。

そんなあなたに functions.php 等に以下を追加すれば一気に解決!

functions.php
// 投稿が一個しかないのでページ送りした際にエラーになるのを回避
add_action( 'pre_get_posts', 'pager_many_many_tester_offset' );
function pager_many_many_tester_offset( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        $query->set( 'offset', 0 );
    }
    return $query;
}

// 投稿を9999にする
add_filter( 'found_posts', 'pager_many_many_tester_found_posts' );
function pager_many_many_tester_found_posts( $found_posts ) {
    $found_posts = 9999;
    return $found_posts;
}

こうすれば、WP-PageNaviでも
スクリーンショット 2016-10-17 19.16.10.png
paginate_links を使ったページネーションでも
スクリーンショット 2016-10-17 19.16.20.png
簡単にテストする事ができます。

以上、良いページネーションライフを。

15
16
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
15
16