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?

wordpressでページネーションを作る方法

Posted at

WordPressでページネーション(ページ送り)を作成する方法はいくつかあります。

paginate_links() を使う方法

WordPressには標準でページネーションを作成する関数です。
WP_Query や query_posts() を使ってカスタムクエリを作成する場合に便利です。

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$args = array(
    'post_type'      => 'post',
    'posts_per_page' => 5,
    'paged'          => $paged,
);

$the_query = new WP_Query($args);
?>

<?php if ($the_query->have_posts()) : ?>
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <p><?php the_excerpt(); ?></p>
    <?php endwhile; ?>

    <div class="pagination">
        <?php
        echo paginate_links(array(
            'total'   => $the_query->max_num_pages,
            'current' => $paged,
            'prev_text' => '« 前へ',
            'next_text' => '次へ »',
        ));
        ?>
    </div>

    <?php wp_reset_postdata(); ?>
<?php else : ?>
    <p>投稿が見つかりませんでした。</p>
<?php endif; ?>

  • paged パラメータを設定して現在のページ番号を取得
  • WP_Query でカスタムクエリを作成
  • paginate_links() でページネーションを表示
  • wp_reset_postdata() でクエリをリセット

the_posts_pagination() を使う方法(標準ループ)

通常の投稿ループ(メインクエリ)を使用している場合は、 the_posts_pagination() 。

<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <p><?php the_excerpt(); ?></p>
    <?php endwhile; ?>

    <div class="pagination">
        <?php
        the_posts_pagination(array(
            'mid_size'  => 2,
            'prev_text' => '« 前へ',
            'next_text' => '次へ »',
        ));
        ?>
    </div>

<?php else : ?>
    <p>投稿が見つかりませんでした。</p>
<?php endif; ?>
  • メインクエリを使っている場合は the_posts_pagination() で簡単に実装可能
  • mid_size は現在のページの前後に表示するページ番号の数

previous_posts_link() と next_posts_link() を使う方法

前後ページの場合。

<?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <p><?php the_excerpt(); ?></p>
    <?php endwhile; ?>

    <div class="pagination">
        <div class="nav-previous"><?php next_posts_link('« 前のページ'); ?></div>
        <div class="nav-next"><?php previous_posts_link('次のページ »'); ?></div>
    </div>

<?php else : ?>
    <p>投稿が見つかりませんでした。</p>
<?php endif; ?>
  • next_posts_link() は「前のページ」へリンク(古い投稿を表示)
  • previous_posts_link() は「次のページ」へリンク(新しい投稿を表示)

プラグインを使う方法

おすすめのプラグイン

  • WP-PageNavi
    → シンプルなページネーションを追加できるプラグイン
  • Pagination by BestWebSoft
    → 設定画面からページネーションのデザインを調整可能

WP-PageNavi の使用例

/*functions.php*/
function custom_pagination($query) {
    if ($query->max_num_pages > 1) {
        echo '<div class="pagination">';
        echo paginate_links(array(
            'total' => $query->max_num_pages,
            'current' => max(1, get_query_var('paged')),
            'prev_text' => '« 前へ',
            'next_text' => '次へ »',
        ));
        echo '</div>';
    }
}

/*index.phpなどの投稿一覧*/
custom_pagination($the_query);
1 2 3
使いやすさ カスタマイズ性
paginate_links()
the_posts_pagination()
previous_posts_link() / next_posts_link()
プラグイン(WP-PageNavi)

どれを使うべき?

  • 標準の投稿一覧(メインクエリ) → the_posts_pagination()
  • カスタムクエリ → paginate_links()
  • シンプルな前後ナビ → previous_posts_link() / next_posts_link()
  • 簡単に済ませたい → WP-PageNavi プラグイン
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?