• 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細

このWikiはいつでも誰でも編集できます

ページネーション

提供: WordPress Codex 日本語版
2015年6月29日 (月) 10:45時点におけるMiccweb (トーク | 投稿記録)による版 (Paginationページネーションへ移動)

移動先: 案内検索

このページ「ページネーション」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

ページ名検討中: このページ名「ページネーション」について改名が提案されています。ご意見をお寄せください。(2015-6-27)

WordPress has the ability to split lists of posts or a single post into multiple pages for "paged" navigation. You can set how many posts to list on each page on the 表示設定/en画面 (wp-admin > Settings > Reading). The “Blog pages show at most” value will be used by WordPress unless your theme overrides it, such as when it is using a custom query. When multiple ループ (posts lists) are used in a theme template file only one loop, the main loop, can be paginated.

プラグインによるページネーション

Most themes will have “previous page” and “next page” links to allow you to navigate through to the older posts. Plugins make it easy to have Numbered pagination instead. Some themes have a pagination plugin included in the theme itself. See the プラグインの章 for plugins that are available and how to troubleshoot some of them.

関数リファレンス

複数投稿のページネーション
個別投稿のページネーション


ページネーションを含むループの例

This simplified example shows where you can add ページネーション関数 for the メインループ. Add the functions just before or after the loop.

<?php if ( have_posts() ) : ?>

<!-- Add the pagination functions here. -->

<!-- Start of the main loop. -->
<?php while ( have_posts() ) : the_post();  ?>

<!-- the rest of your theme's main loop -->

<?php endwhile; ?>
<!-- End of the main loop -->

<!-- Add the pagination functions here. -->

<div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
<div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

See この例 if you're querying the loop with WP_Query.

ページネーションの問題解決

Sometimes pagination will break and give unexpected results, redirect you to the wrong page or give you a 404 (page not found) on the "paged" pages. This is usually due to your theme altering (querying) the メインループ wrong.

問題解決の基本ステップ

De-activate all プラグイン to rule out plugins breaking the pagination. If this works, re-activate the plugins one by one until you find the problematic plugin(s). If you are using plugins for your pagination see the プラグインの章 of this page.

Re-save your permalink structure on the パーマリンク設定/en画面 (wp-admin > Settings > Permalinks) or set it to the default structure to see if that fixed it.

Ask for help in the フォーラム if you find the "Advanced Troubleshooting Steps" below a bit too technical or if it didn't help solve your broken pagination. Provide links if possible to a page demonstrating the problem, and where you downloaded your theme from. State what troubleshooting steps you did or did not do. Bonus points if you have read the フォーラムへようこそ/en, searched for a solution yourself, located and submitted the template file where the pagination is broken to a pastebin/en and added a link to it in your forum topic.

問題解決の上級ステップ

The next steps are for when your theme is querying the loop and is using one of the 複数投稿のページネーション functions.

For this advanced section it is assumed you have a basic understanding of the following:

Important! Make a バックアップ/en of your theme template files and create a 子テーマ/en before editing them.

ステップ 1. テンプレートファイルを特定する

The first thing we need to know, is on what theme template file the pagination is broken. Looking at the テンプレート階層 can help you figure out which テンプレートファイル is used. Or locate the file with the use of a plugin.

ステップ 2. ページネーションされたメインループを見つける

Open the template file that you found in the previous step and find the main loop. The main loop (if there is more then one loop) is the one that has one of the 複数投稿のページネーション functions before and/or after it. Your theme may be using a plugin or some other method of pagination if you didn't find any of the functions.

クエリに "paged" パラメーターを追加

If query_posts or WP_Query is altering the main loop and the "paged" parameter is not set you'll need to add it with get_query_var(). This is so WordPress knows exactly what page it's on.

For example, if your query looks like this (without the "paged" parameter):

<?php query_posts('posts_per_page=3'); ?>

you add the parameter like this:

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

query_posts('posts_per_page=3&paged=' . $paged); 
?>

The next example is exactly the same as above but with the parameters in an array:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
  'posts_per_page' => 3,
  'paged' => $paged
);

query_posts($args); 
?>

変数を渡す to query posts() or new WP_Query().

静的フロントページ

If the pagination is broken on a 静的フロントページ you have to add the "paged" parameter this way:

<?php 
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }

query_posts('posts_per_page=3&paged=' . $paged); 
?>

メインループから query_posts を排除

query_posts を使わないメインループの変更方法について、詳しくは外部リソースをご覧ください。

query_posts() is the easiest, but not the preferred or most efficient, way to alter the default query that WordPress uses to display posts. Usually it's also responsible for breaking pagination. If your theme is using it to query the main loop you can replace it with the preferred way, that is to say, hooking into 'pre_get_posts' and altering the main query by using is_main_query(). This way is more reliable and faster because the query for the main loop is altered before the posts are retrieved from the database. This method works on all template files except ページテンプレート. See this 用例 on how to do a query on a Page template by using WP_Query.

For example, lets say your theme queries the main loop like this on your home page (index.php) and category pages (category.php) and the pagination is not working:

<?php 
// the query to set the posts per page to 3
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
query_posts($args); ?>
<!-- the loop -->
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
		<!-- rest of the loop -->
		<!-- the title, the content etc.. -->
<?php endwhile; ?>
<!-- pagination -->
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php else : ?>
<!-- No posts found -->
<?php endif; ?>

Remove the query_posts part from the template files (index.php, category.php) used in this example:

<?php 
// query to set the posts per page to 3
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 3, 'paged' => $paged );
query_posts($args); ?>

And add the query for your home and category pages back in your theme's functions.php/en file:

function my_post_queries( $query ) {
  // do not alter the query on wp-admin pages and only alter it if it's the main query
  if (!is_admin() && $query->is_main_query()){

    // alter the query for the home and category pages 

    if(is_home()){
      $query->set('posts_per_page', 3);
    }

    if(is_category()){
      $query->set('posts_per_page', 3);
    }

  }
}
add_action( 'pre_get_posts', 'my_post_queries' );

As you can see we can make use of 条件分岐タグ (is_home(), is_category(), etc...). This lets us target the pages where we want to alter the query. Lets say you want 6 posts on a category page called "Movies". We can add this to the my_post_queries() function above to target that category:

    // alter the query for the Movies category page 
    if(is_category('Movies')){
      $query->set('posts_per_page', 6);
    }

Read more about querying with pre_get_posts:

プラグイン

There are many pagination plugins to choose from in the プラグイン ディレクトリ. Two of the more popular ones that allow you to have numbered pagination are wp-pagenavi and wp-paginate.

プラグインの問題解決

De-activate the pagination plugin and 問題解決を実施 with one of the WordPress ページネーション functions. Usually if it works after that it will also work with the plugin.

wp-pagenavi の問題解決

These instructions are from one of the plugin authors:

外部リソース

ページネーション: posts_nav_link(), next_posts_link(), previous_posts_link(), get_next_posts_link(), get_previous_posts_link(), paginate_links(), wp_link_pages(), next_post_link(), previous_post_link(), next_image_link(), previous_image_link(), the_posts_pagination(), get_the_posts_pagination()

最新英語版: WordPress Codex » Pagination最新版との差分