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

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

The Loop

提供: WordPress Codex 日本語版
2008年3月30日 (日) 18:52時点におけるBono (トーク | 投稿記録)による版 (2 版)

移動先: 案内検索

WordPressループ」とは、記事を表示させるために使うPHPコードのセットのことです。WordPressはこの「ループ」を使って、現在のページに表示される各記事を処理したり、ループタグ内で指定された基準にそって記事の形式を整えたりします。「ループ」の開始部分と終了部分の間に書きこんだ HTMLPHP のコードは、各記事の表示に使われます。

テンプレートタグやプラグインの説明内に、「このタグ(プラグイン)はループ内で使います」とある場合、このWordPressタグのことを指しています。

以下はループ内に含める記事の情報例です。

記事のタイトル(the_title()) 記事の公開日時(the_time()) 属するカテゴリー(the_category()) 記事の本文(the_content()

さらに他のテンプレートタグを使ったり$post変数にアクセスしたりして、記事に関する様々な情報を表示することもできます。

ループに始めて触れる人は、ループの使用例ページも参照してください。

WordPressループの使い方

WordPressループは、 index.phparchive.php など、複数の記事を表示させるためのテンプレートに挿入して使います。WordPressのバージョンにより多少書式が異なりますので、インストールしたWordPressのバージョンを確認しましょう。

WordPress 1.5縲鰀2.x

現在使用中のテーマテンプレートの最初に、ヘッダーテンプレートを呼び出しておくのを忘れないようにしましょう。もし独自のデザイン内にWordPressループを挿入する場合、以下のようにしてWP_USE_THEMESfalse に設定しておきます。

<?php define('WP_USE_THEMES', false); get_header(); ?>

WordPressループは以下の行から始まります。

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

以下の行が、ループの終わりです。

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

WordPress 1.2

indexページの最初に、 wp-blog-header.php をインクルードしておきます。その際、同ファイルへの正しいパスを記入しましょう。

<?php /* Don't remove this line. */ require('./wp-blog-header.php'); ?>

WordPressループは以下の行から始まります。

<?php if ( $posts ) : foreach ( $posts as $post ) : start_wp(); ?>

以下の行が、ループの終わりです。

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

WordPressループの例

特定のカテゴリにある記事のスタイルを変更する

以下の説明は、WordPress v1.5 以上向けです。

以下のコードは、 in_category() テンプレートタグを使ってカテゴリー番号が3の記事に他とは別のCSSクラス名を割り当てる場合の例です。CSS(style.css)内でこのクラスの宣言を追加・編集することにより、カテゴリー3の記事にのみ特別なスタイルを加えることができます。

<!-- --> というHTMLコメントで囲まれた部分は、実際にブラウザでは表示されません。ここではコードを分かりやすくするためにHTMLコメントで囲んだ説明を挿入していますが、実際にはテンプレート内に書き込まなくてもかまいません。

 <!-- WordPressループ開始 -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 <!-- 以下で、記事がカテゴリー3に属しているかテスト -->
 <!-- もし属している場合、"post-cat-three"というCSSクラスのdivボックスを表示 -->
 <!-- それ以外の場合、"post"というCSSクラスのdivボックスを表示 -->
 <?php if ( in_category('3') ) { ?>
           <div class="post-cat-three">
 <?php } else { ?>
           <div class="post">
 <?php } ?>

 <!-- 記事のタイトルとパーマリンクを表示 -->
 <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

 <!-- 日時を表示 -->
 <small><?php the_time('F jS, Y'); ?></small>

 <!-- 記事の本文をdiv内に表示 -->
 <div class="entry">
  <?php the_content(); ?>
 </div>

 <!-- 記事のカテゴリーをコンマ区切りで表示 -->
 <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
 </div> <!-- 最初の div ボックスを閉じる -->

 <!-- “else”部分を除いたWordPressループ終了 -->
 <?php endwhile; else: ?>

 <!-- 最初の“if”にて表示する記事があるかどうかをテストしたため、“else”では記事がない場合に実行 -->
 <!-- つまり、記事がなければ以下を表示 -->
 <p>Sorry, no posts matched your criteria.</p>

 <!-- WordPressループを「完全に」終了 -->
 <?php endif; ?>

注: HTMLコードをテンプレート内に書く場合は、必ず <?php  ?> というPHP開始・終了タグの外側に書かなければなりません。逆に、PHPコードは、必ず <?php  ?> タグの内側に書きます。上記のように、ifelse ステートメント内でもPHPコードを一時的に閉じ、HTMLコードを書くことができます。

Exclude Posts From Some Category

For WordPress v1.5 Only

This example can be used to exclude a certain Category from being displayed. It is based on the example above.

 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

 <!-- If the post is in the category we want to exclude, we simply pass to the next post. -->
 <?php if (in_category('3')) continue; ?>
 
 <div class="post">
 
  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
 
  <small><?php the_time('F jS, Y'); ?></small>
 
  <div class="entry">
    <?php the_content(); ?>
  </div>

  <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
 </div> <!-- closes the first div box -->

 <?php endwhile; else: ?>
 <p>Sorry, no posts matched your criteria.</p>
 <?php endif; ?>

Note: If you use this example for your main page, you should use a different Template for your Category archives. Otherwise, WordPress will exclude all posts in Category 3 even when viewing that Category Archive!

However, if you want to use the same template file, you can avoid this by using the is_home() tag:

...
<?php if (in_category('3') && is_home() ) continue; ?>
...

This will ensure that posts from Category 3 will only be excluded from the main page. There are other Conditional Tags that can be used to control the output depending on whether or not a particular condition is true with respect to the requested page.

Please note that even though the post is not being displayed it is still being counted by WordPress as having been shown -- this means that if you have WordPress set to show at most seven posts and that two of the last seven are from Category 3 then you will only display five posts on your main page. If this is a problem for you, there is more complicated hack you can employ described in the Layout and Design FAQ or you can use query_posts if you only need to exclude one category from the loop.

Multiple Loops

This section deals with advanced use of The Loop. It's a bit technical 窶骭€ but don’t let that scare you. We’ll start off at easy and work up from there. With a little common sense, patience, and enthusiasm, you too can do multiple loops.

First off, "why would one want to use multiple loops?" In general, the answer is that you might want to do something with one group of posts, and do something different to another group of posts, but display both groups on the same page. Something could mean almost anything; you are only limited by your PHP skill and your imagination.

We will get into examples below, but first you should read about the basics. Take a look at the basic Loop. It consists of:

     <?php if (have_posts()) : ?>
               <?php while (have_posts()) : the_post(); ?>    
     <!-- do stuff ... -->
     <?php endwhile; ?>

In English (PHP types and people familiar with code speak can skip to below): To translate to English, the above would be read: If we are going to be displaying posts, then get them, one at a time. For each post in the list, display it according to <!-- do stuff ... -->. When you hit the last post, stop. The do stuff line(s), are template dependent.

Aside on Do stuff: in this example it is simply a placeholder for a bunch of code that determines how to format and display each post on a page. This code can change depending on how you want your WordPress to look. If you look at the Kubrick theme’s index.php the do stuff section would be everything below:

     <?php while (have_posts()) : the_post(); ?>

To above:

     <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?>

An explanation for the coders out there: The have_posts() and the_post() are convenience wrappers around the global $wp_query object, which is where all of the action is. The $wp_query is called in the blog header and fed query arguments coming in through GET and PATH_INFO. The $wp_query takes the arguments and builds and executes a DB query that results in an array of posts. This array is stored in the object and also returned back to the blog header where it is stuffed into the global $posts array (for backward compatibility with old post loops).

Once WordPress has finished loading the blog header and is descending into the template, we arrive at our post Loop. The have_posts() simply calls into $wp_query->have_posts() which checks a loop counter to see if there are any posts left in the post array. And the_post() calls $wp_query->the_post() which advances the loop counter and sets up the global $post variable as well as all of the global post data. Once we have exhausted the loop, have_posts() will return false and we are done.

Loop Examples

Below are two example of using multiple loops. The key to using multiple loops is that $wp_query can only be called once. In order to get around this it is possible to re-use the query by calling rewind_posts() or by creating a new query object. This is covered in example 1. In example 2, using a variable to store the results of a query is covered. Example 3 documents the use of update_post_caches(); function to avoid common plugin problems. Finally, ‘multiple loops in action’ brings a bunch of ideas together to document one way of using multiple loops to promote posts of a certain category on your blog’s homepage.

Multiple Loops Example 1

In order to loop through the same query a second time, call rewind_posts(). This will reset the loop counter and allow you to do another loop.

  <?php rewind_posts(); ?>
 
  <?php while (have_posts()) : the_post(); ?>
    <!-- Do stuff... -->
  <?php endwhile; ?>

If you are finished with the posts in the original query, and you want to use a different query, you can reuse the $wp_query object by calling query_posts() and then looping back through. The query_posts() will perform a new query, build a new posts array, and reset the loop counter.

  // Get the last 10 posts in the special_cat category.
  <?php query_posts('category_name=special_cat&showposts=10'); ?>

  <?php while (have_posts()) : the_post(); ?>
    <!-- Do special_cat stuff... -->
  <?php endwhile;?>

If you need to keep the original query around, you can create a new query object.

<?php $my_query = new WP_Query('category_name=special_cat&showposts=10'); ?>

<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  <!-- Do special_cat stuff... -->
<?php endwhile; ?>

The query object my_query is used because you cannot use the global have_posts() and the_post() since they both use $wp_query. Instead, call into your new $my_query object.

Multiple Loops Example 2

Another version of using multiple Loops takes another tack for getting around the inability to use have_posts() and the_post(). To solve this, you need to store the original query in a variable, then re-assign it when with the other Loop. This way, you can use all the standard functions that rely on all the globals.

For example:

// going off on my own here
<?php $temp_query = $wp_query; ?>
<!-- Do stuff... -->

<?php query_posts('category_name=special_cat&showposts=10'); ?>

<?php while (have_posts()) : the_post(); ?>
  <!-- Do special_cat stuff... -->
<?php endwhile; ?>

// now back to our regularly scheduled programming
<?php $wp_query = $temp_query; ?>

Note: In PHP 5, objects are referenced with the "="-operator instead of copied like in php4. To make Example 2 work in PHP 5 you need to use the following code:

 // going off on my own here
 <?php $temp_query = clone $wp_query; ?>
 <!-- Do stuff... -->
 
 <?php query_posts('category_name=special_cat&showposts=10'); ?>
 
 <?php while (have_posts()) : the_post(); ?>
   <!-- Do special_cat stuff... -->
 <?php endwhile; ?>
 
 // now back to our regularly scheduled programming
 <?php $wp_query = clone $temp_query; ?>
Multiple Loops Example 3 - Plugins

It has been found that some plugins don’t play nice with multiple loops. In these cases it was discovered that some plugins which deal with the keyword(s) and tagging of posts, only work for the first instance of a loop in a page where that loop consists of a subset of total posts. If you find that this is the case, you might want to try the following implementation of the basic loop which adds the update_post_caches($posts) function. This function resets the post cache and is as yet undocumented. This implementation would be used on the second loop in a page only if the first loop retrieves a subset of posts.

Simply amend:

   <?php if (have_posts()) : ?>
               <?php while (have_posts()) : the_post(); ?>    
     <!-- Do stuff... -->
   <?php endwhile; ?>

to become:

   <?php if (have_posts()) : ?>
               <?php while (have_posts()) : the_post(); update_post_caches($posts); ?>
     <!-- Do stuff... -->
   <?php endwhile; ?>

Multiple Loops in Action

The best way to understand how to use multiple loops is to actually show an example of its use. Perhaps the most common use of multiple loops is to show two (or more) lists of posts on one page. This is often done when a webmaster wants to feature not only the very latest post written, but also posts from a certain category.

Leaving all formatting and CSS issues aside, let us assume we want to have two lists of posts. One which would list the most recent posts (the standard 10 posts most recently added), and another which would contain only one post from the category ‘featured’. Posts in the ‘featured’ category should be shown first, followed by the second listing of posts (the standard). The catch is that no post should appear in both categories.

Step 1. Get only one post from the ‘featured’ category.

  <?php $my_query = new WP_Query('category_name=featured&showposts=1');
  while ($my_query->have_posts()) : $my_query->the_post();
  $do_not_duplicate = $post->ID; ?>
    <!-- Do stuff... -->
  <?php endwhile; ?>

In English the above code would read:

Set $my_query equal to the result of querying all posts where the category is named featured and by the way, get me one post only. Also, set the variable $do_not_duplicate equal to the ID number of the single post returned. Recall that the Do stuff line represents all the formatting options associated for the post retrieved.

Note that we will need the value of $do_not_duplicate in the next step to ensure that the same post doesn't appear in both lists.

Step 2. The second loop, get the X latest posts (except one).

The following code gets X recent posts (as defined in WordPress preferences) save the one already displayed from the first loop and displays them according to Do stuff.

  <?php if (have_posts()) : while (have_posts()) : the_post(); 
  if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
   <!-- Do stuff... -->
  <?php endwhile; endif; ?>

In English the above code would read:

Get all posts, where a post equals $do_not_duplicate then just do nothing (continue), otherwise display all the other the posts according to Do stuff. Also, update the cache so the tagging and keyword plugins play nice. Recall, $do_not_duplicate variable contains the ID of the post already displayed.

The End Result

Here is what the final piece of code looks like without any formatting:

  <?php $my_query = new WP_Query('category_name=featured&showposts=1');
  while ($my_query->have_posts()) : $my_query->the_post();
  $do_not_duplicate = $post->ID;?>
    <!-- Do stuff... -->
  <?php endwhile; ?>
    <!-- Do other stuff... -->
  <?php if (have_posts()) : while (have_posts()) : the_post(); 
  if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
   <!-- Do stuff... -->
  <?php endwhile; endif; ?>

The end result would be a page with two lists. The first list contains only one post -- the most recent post from the 'feature' category. The second list will contain X recent posts (as defined in WordPress preferences) except the post that is already shown in the first list. So, once the feature post is replaced with a new one, the previous feature will show up in standard post list section below (depending on how many posts you choose to display and on the post frequency). This technique (or similar) has been used by many in conjunction with knowledge of the Template Hierarchy to create a different look for home.php and index.php. See associated resources at the bottom of this page.

Sources:

This article on multiple loops is a combination of Ryan Boren and Alex King's discussion about the Loop on the Hackers Mailing List as well as the tutorial written at MaxPower.

More Loop Resources

To learn more about the WordPress Loop, and the various template tags that work only within the Loop, here are more resources.

Resources

この記事は翻訳時に編集が必要であるとマークされていました。その為Codex原文が大きく編集されている可能性があります。内容を確認される際は原文を参照していただき、可能であれば本項目へ反映させてください。よりよいCodexを作成するためのお手伝いをお願いします。