WordPressのLoop

Sponsored Link

ループはあなたのポストの各々を表示するためにWordPressによって使用されます。ループの使用、WordPressプロセス各々、現在のページに表示されるポストの、また、ループ・タグ内の指定された基準とどのように一致するかによってそれらをフォーマットします。ループに置かれたどんなHTMLまたはPHPのコードも、各ポスト上で繰り返されるでしょう。「このタグはループ内にあるに違いない」とWordPressドキュメンテーションが述べる時、特定のテンプレート・タグあるいはpluginsのためにのように、タグは各ポスト用に繰り返されるでしょう。

Sponsored Link

原文The Loop is used by WordPress to display each of your posts. Using The Loop, WordPress processes each of the posts to be displayed on the current page and formats them according to how they match specified criteria within The Loop tags. Any HTML or PHP code placed in the Loop will be repeated on each post. When WordPress documentation states “This tag must be within The Loop”, such as for specific Template Tag or plugins, the tag will be repeated for each post.

例えば、情報の中で、WordPress 1.5の中のデフォルトによるループ・ディスプレイは次のとおりです:各ポストのタイトル(the_title())、タイム(the_time())およびカテゴリー(the_category())。各ポストに関する他の情報は、適切なテンプレートタグで、あるいは(上級ユーザのために)$post変数へのアクセスによって表示することができます。ループが走っている間、それは現在のポストの情報で飾られます。

原文For example, among the information The Loop displays by default in WordPress 1.5 are: the Title (the_title()), Time (the_time()), and Categories (the_category()) for each post. Other information about each post can be displayed with the appropriate Template Tags or (for advanced users) by accessing the $post variable, which is set with the current post’s information while The Loop is running.

ループへの初心者注視については、ループを活動して参照してください。

原文For a beginners look at The Loop, see The Loop in Action.

ループの使用

Using The Loop

ループは、index.php、およびポスト情報を表示するために使用される他のテンプレートに置かれるべきですが、WordPressのあなたのバージョン上のわずかに異なるように依存することを見ます。最初に「あなたがWordPressのどのバージョンを持っているか知るべきです。」

原文The Loop should be placed in index.php and in any other Templates used to display post information, but looks slightly different depending on your version of WordPress. You should first “find what version of WordPress you have”.

WordPress 2.0

これは恐らくWordPress 1.5と同じです。

原文This is (probably) the same as WordPress 1.5

WordPress 1.5

必ずテーマ・テンプレートの一番上のヘッダー・テンプレートの要請を含めてください。自分の設計(また、自分の設計はテンプレートではありません)の内部のループを使用している場合は、誤りのことにWP_USE_THEMESをセットしてください。

原文Be sure to include the call for the header Template at the top of your Theme’s Templates. If you are using The Loop inside your own design (and your own design is not a template), set WP_USE_THEMES to false.

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

ループはここでスタートします:

原文The loop starts here:

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

そして、ここで終了します:

原文and ends here:

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

WordPress 1.2

必ずインデックス・ページの一番上のwp-blog-header.phpの要請を含めてください。あなたのwp-ブロッグ-header.phpファイルの位置にwp-blog-header.phpのためのパスがセットされるに違いないことを覚えておいてください:

原文Be sure to include the call for wp-blog-header.php at the top of your index page. Remember, the path for wp-blog-header.php must be set to the location of your wp-blog-header.php file:

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

ループはここでスタートします:

原文The loop starts here:

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

そして、ここで終了します:

原文and ends here:

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

ループ例

Loop Examples

スタイルは、あるカテゴリーから違った風に記入します

原文Style Posts From Some Category Differently

WordPress v1.5のみのために

原文For WordPress v1.5 Only

シンタックスをバージョン1.5に使用するこの例は、そのタイトル(それはポストPermalinkへのリンクとして使用される)、カテゴリーおよび内容を備えた各ポストを表示します。それは単純で貧弱な例です;恐らく、あなたのテンプレートはCSSで事態をスタイルにとってより簡単にして、ある意味ではより多くの情報を表示するでしょう。

原文This example, using the syntax for version 1.5, displays each post with its Title (which is used as a link to the Post’s Permalink), Categories, and Content. It is a simple, bare-bones example; likely your Templates will display more information in a way making things easier to style with CSS.

しかしながら、少しより有益なことを、この例は、さらにカテゴリーID「3」を備えたカテゴリーのポストが違った風にデザインされることを可能にします。これを遂行するために、in_category()テンプレート・タグは使用されます。

原文In order to be a little more instructive, though, this example also allows posts in a category with Category ID ‘3’ to be styled differently. To accomplish this, the in_category() Template Tag is used.

そのタグはHTMLコメント・タグです。あなたがこの例を使用すれば、これらのタグはウェブ・ブラウザに表示しません。それらはコードを下に注釈すること以外に目的に役立ちません。

原文The tags are HTML comment tags; if you use this example, these tags will not display in web browsers. They serve no purpose other than to annotate the code below.

<!-- Start the Loop. -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<!-- The following tests if the current post is in category 3. -->
<!-- If it is, the div box is given the CSS class "post-cat-three". -->
<!-- Otherwise, the div box will be given the CSS class "post". -->
<?php if ( in_category('3') ) { ?>
<div class="post-cat-three">
  <?php } else { ?>
  <div class="post">
    <?php } ?>
    <!-- Display the Title as a link to the Post's permalink. -->
    <h2><a href="<?php the_permalink(); ?>">
      <?php the_title(); ?>
      </a></h2>
    <!-- Display the Time. -->
    <small>
    <?php the_time('F jS, Y'); ?>
    </small>
    <!-- Display the Post's Content in a div box. -->
    <div class="entry">
      <?php the_content(); ?>
    </div>
    <!-- Display a comma separated list of the Post's Categories. -->
    <p class="postmetadata">Posted in
      <?php the_category(', '); ?>
    </p>
  </div>
  <!-- closes the first div box -->
  <!-- Stop The Loop (but note the "else:" - see next line). -->
  <?php endwhile; else: ?>
  <!-- The very first "if" tested to see if there were any Posts to -->
  <!-- display.  This "else" part tells what do if there weren't any. -->
  <p>Sorry, no posts matched your criteria.</p>
  <!-- REALLY stop The Loop. -->
  <?php endif; ?>
  

注:いつでも、あなたはHTMLコードを使用したい、あなたは外にいるに違いありませんタグ。PHPコード(カール状の締め金ほど単純なものさえ: })は内側にあるに違いありませんタグ。PHPを始めて止めることができます、HTMLを埋め込むためにコード化する、コード化する、の内にさえ、場合、そしてほかに、ステートメント、上記の例において示されたように。

原文Note: Anytime you want to use HTML code, you must be outside the tags. PHP code (even things as simple as curly braces: } ) must be inside the tags. You can start and stop the PHP code in order to intersperse HTML code even within if and else statements, as shown in the above example.

あるカテゴリーからポストを除外します

原文Exclude Posts From Some Category

WordPress v1.5のために、そしてより高い

原文For WordPress v1.5 and higher

この例は表示からあるカテゴリーを除外するために使用することができます。それは上記の例に基づきます。

原文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; ?>
  

注:この例を主なページに使用すれば、異なるテンプレートをカテゴリー・アーカイブに使用するべきです。そうでなければ、そのカテゴリー・アーカイブを見る時さえ、WordPressは、カテゴリー3のポストをすべて除外するでしょう!

原文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!

しかしながら、同じテンプレート・ファイルを使用したければ、is_home()タグの使用によりこれを回避することができます:

原文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') &amp;&amp; is_home() ) continue; ?>
  ...
  

これは、カテゴリー3からのポストが単に主なページから除外されるだろうということを保証するでしょう。特別の条件が要求されたページに関して真実かどうかに依存する出力をコントロールするために使用することができる他の条件付きのタグがあります。

原文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.

たとえポストが表示されていなくても、示されるようにそれがWordPressによってまだ数えられていることに注意してください — これは、WordPressを高々7つのポストを示すのに取りかからせ、最後の7つのその2つが次にカテゴリー3からならば、あなたが単に主なページに5つのポストを表示するだろうということを意味します。これがあなたにとって問題となる場合、レイアウトおよび設計FAQに記述されて使用することができる、より複雑な荒傷があります。あるいは、単にループから1つのカテゴリーを除外する必要があれば、query_postsを使用してもよい。

原文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.

ファースト、から、「なぜ一人は多数のループを使用したいと思うでしょうのですか。」一般に、答えは、1グループのポストを備えた何かをしたいかもしれないし、ポストの別のグループと異なるものをしたいかもしれないが、同じページに両方のグループを表示するということです。何かが何でもほとんど意味するかもしれません;PHP技術および想像によって制限されていますだけです。

原文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; ?>
  

英語(コードに身近なタイプと人々が話すPHPは飛ばすことができます。
)で、上記は読まれるでしょう: 私たちがポストを表示するなら、一度に1にそれらを手に入れてください。 リストの中の各ポストには、に従って、それを表示してください。 軍葬ラッパに達したとき、止まってください。 線を詰めてください、そして、テンプレート扶養家族はそうです

原文In English (PHP types and people familiar with code speak can skip to below), 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 . When you hit the last post, stop. The do stuff line(s), are template dependent.

わきに、次のものを詰める:この例において、それは、単にページの各ポストをフォーマットし表示する方法を決定するコードの房のため?フプレースホールダーです。このコードはあなたがどのようにWordPressが見ることを望むかに依存して変わる場合があります。あなたが、doが詰めるKubrick・テーマのindex.phpを見れば、セクションは下のようなすべてになるでしょう:

原文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 ≫'); ?>
  

コーダーの説明、そこに:have_posts()およびthe_post()はグローバルな$wp_queryオブジェクトのまわりの便宜包み紙です。それはアクションがすべてそうである場所です。$wp_queryは、GETとPATH_INFOを通って中へ入る、ブロッグ・ヘッダーおよび供給された質問議論の中で呼ばれます。$wp_queryは議論をとり、多くのポストに帰着するDB質問を構築し、実行します。この配列はオブジェクトに格納され、それが、グローバルな$posts配列(古いポスト・ループとの後方互換性用の)に詰め込まれる場合、ブロッグ・ヘッダーに返されます。

原文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).

一旦WordPressがブロッグ・ヘッダーをロードし終えており、テンプレートへ下っていれば、私たちはポスト・ループに到着します。have_posts()は、単に、いずれかのポストがポスト配列の中に残されているかどうか確かめるためにループ・カウンタをチェックする$wp_query->have_posts()へ呼びます。また、the_post()は、ループ・カウンタを進めて、グローバルなポスト・データのすべてと同様にグローバルな$ポスト変数もセット・アップする$wp_query->have_posts()に電話します。一旦私たちがループを消耗したならば、()が誤りに返すhave_postsおよび私たちは終っています。

原文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

以下に、多数のループを使用する2つの例です。多数のループの使用の鍵は$wp_queryを一度だけ呼ぶことができるということです。このあたりに行くために、rewind_posts()を呼ぶことにより、あるいは新しい質問オブジェクトの作成により再度質問を使用することは可能です。これは例1でカバーされます。例2において、質問の結果を格納するために変数を使用することは、カバーされます。例3は、update_post_caches()の使用を文書化します;共通のplugin問題を回避する機能。最後に、「活動している多数のループ」は、あなたのブロッグのホームページ上のあるカテゴリーとしての地位を促進するために多数のループを使用する1つの方法を文書化するために考えの房を集めます。

原文Below are two examples 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 the 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

同じ質問によって2回目をループにするためには、rewind_posts()を呼んでください。これはループ・カウンタをリセットし、あなたが別のループを行うことを可能にするでしょう。

原文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; ?>
  

オリジナル質問中のポストを終えて、あなたが異なる質問を使用したければ、query_posts()を呼び、次に、後ろに通り抜けてループすることにより、$wp_queryオブジェクトを再使用することができます。query_posts()は新しい質問を行ない、新しいポストに配列を作ってやり、ループ・カウンタをリセットするでしょう。

原文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&amp;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&amp;showposts=10'); ?>
  <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  <!-- Do special_cat stuff... -->
  <?php endwhile; ?>
  

それらが両方とも$wp_queryを使用するのであなたがグローバルなhave_posts()およびthe_post()を使用することができないので、質問オブジェクトmy_queryが使用されます。代わりに、新しい$my_query目的へ呼んでください。

原文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

多数のループを使用する別のバージョンは、have_posts()およびthe_post()を使用する無力のあたりに行くために別の鋲をとります。これを解決するために、変数にオリジナルの質問を格納し、次に、別のループでいる場合、再度それを割り当てる必要があります。このように、すべてのglobalsに依存する標準関数をすべて使用してもよい。

原文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&amp;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&amp;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; ?>
  

しかしながら、この第2の例はWordPress 2.1で働きません。

原文However, this second example does not work in WordPress 2.1.

Multiple Loops Example 3 – Plugins

あるpluginsが遊ばないことは知られました、多数のループでよいこれらの場合では、キーワードに対処するあるpluginsおよびポストの付きまとうことが、そのループがポストの合計の部分集合から成るページのループの最初の実例のために働くだけのことが発見されました。これがそうであることを知れば、update_post_caches($ポスト)機能を加える基礎的なループの次のインプリメンテーションを試みたいと思うかもしれません。この機能はポスト貯蔵所をリセットし、まだ公文書がありません。最初のループがポストの部分集合を検索する場合のみ、このインプリメンテーションはページに第2のループ上で使用されるでしょう。

原文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

多数のループを使用する方法を理解する最良の方法は実際にその使用の例を示すことです。恐らく、多数のループの中で最も一般的な使用は1つのページのポストの2つの(あるいはより多くの)リストを示すことです。ウェブマスターが書かれたまさに最新のポストだけでなくあるカテゴリーからのポストも特色としたい場合、これはしばしば行われます。

原文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.

フォーマットおよびCSS問題をすべて離れて、私たちがポストの2つのリストを持ちたいと仮定しましょう。最も最近のポスト(最も最近加えられた標準10ポスト)をリストするもの、およびカテゴリーからのたった1つのポストを含む別のものは「重要な役割を演じました。」ポスト、の中で「特色とされた」カテゴリーはポスト(標準)をリストする第2つを後に続けて、最初に示されるべきです。問題点は、ポストが両方のカテゴリーに現われないに違いないということです。

原文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.

ステップ1

たった1つのポストを得る、から「特色とされた」カテゴリー。 Step 1. Get only one post from the ‘featured’ category.

  <?php $my_query = new WP_Query('category_name=featured&amp;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:

特色とされて、カテゴリーが指定される場合にポストをすべて尋ねて、ところで結果と等しいセット$my_query、私に1つのポストのみを取って来ます。さらに、返された単一のポストのID番号と等しい可変$do_not_duplicateをセットしてください。Doがラインを詰めることを思い出す、ポスト用に関連したフォーマット・オプションがすべて検索したと言います。

原文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.

同じポストが両方のリストに現われないことを保証する次のステップで、私たちが$do_not_duplicateの値を必要とするだろうということに注意してください。

原文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.

ステップ2

第2のループ、Xの最新のポスト(一つ以外の)を得ます。

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

次のコードは、最近のポスト(WordPress優先権の中で定義されたとともに)が救うXを得ます、第1のループから既に表示されたもの、またそれらを表示する、によって、詰めます。

原文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:

ポストをすべて得る、どこで、ポスト、同等のもの$do_not_duplicate、その後、単に何(継続する)もしない、そうでなければ他方をすべて表示する、ポスト、によって、詰めます。さらに、よくて、付きまといキーワードpluginsが遊ぶように、貯蔵所を更新してください。思い出す、$do_not_duplicate変数は、既に表示されていたポストのIDを含んでいます。

原文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&amp;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; ?>
  

最終結果は2つのリストを備えたページになるでしょう。最初のリストはたった1つのポストを含んでいます — 「特徴」カテゴリーからの最も最近のポスト。第2のリストは、最初のリスト中で既に示されているポスト以外のXの最近のポスト(WordPress優先権の中で定義されたとともに)を含むでしょう。したがって、一度、特徴ポストは新しいものと取り替えられます、前の特徴は、標準ポスト・リスト・セクションの中で下に(あなたがどれだけのポストに表示することに決めるか、またポスト頻度に依存して)現われるでしょう。home.phpとindex.phpのための異なる外観を作成するために、この技術(あるいは類似した)は、テンプレート階層についての知識と協力する多数によって使用されました。このページの底の関連する資源を参照してください。

原文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.

Note for Multiple Posts in the First Category

原文Note for Multiple Posts in the First Category

原文If showposts=2 or more, you will need to alter the code a bit. The variable $do_not_duplicate needs to be changed into an array as opposed to a single value. Otherwise, the first loop will finish and the variable $do_not_duplicate will equal only the id of the latest post. This will result in duplicated posts in the second loop. To fix the problem replace

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

原文with

  <?php $my_query = new WP_Query('category_name=featured&amp;showposts=2');
  while ($my_query->have_posts()) : $my_query->the_post();
  $do_not_duplicate[] = $post->ID ?>
  

原文Note that “showposts” can be any number. This changes $do_not_duplicate into an array. Then replace

  <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID ==
  $do_not_duplicate ) continue; update_post_caches($posts); ?>
  

原文with

  <?php if (have_posts()) : while (have_posts()) : the_post(); 
 if (in_array($post->ID, $do_not_duplicate)) continue;
 update_post_caches($posts); ?>
  

原文Where you continue the pattern for whatever showposts is set equal to (2 in this case).

原文Alternatively you can pass the entire $do_not_duplicate array to $wp_query and only entries that match your criteria will be returned:

  <?php query_posts(array('post__not_in'=>$do_not_duplicate));
 if (have_posts()) : while (have_posts()) : the_post();
 update_post_caches($posts); ?>
  

原文Note that instead a string, the query parameter was an associative array, with post__not_in option.

ソース

Sources:

多数のループ上のこの記事は、ライアンBoren、およびアレックス・キングのMaxPowerで書かれたチュートリアルと同様にハッカー・メーリング・リスト上のループに関する議論のコンビネーションです。

原文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

ここに、WordPressループ、およびループ内にのみ働く様々なテンプレート・タグに関してもっと学習するために、さらに資源があります。

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

  • The Loop in Action
  • Template Tags
  • Using the Loop in Template Files
  • Matt Read Loop Article
  • MaxPower Dynamic Sticky Tutorial
  • Shaun Andrews’ Sticky Posts that disappear after a few days
  • IfElse Query_post Redux
  • The Loop and Adding Content Outside of It
  • 1001 WordPression Loops
  • Global Variables and the WordPress Loop
  • WordPress Triple Loop Tutorial
  • Super Loop: Exclude Categories and Limit Number of Posts
  • Easily Adaptable WordPress Loop Templates: Basic Loops, Mullet Loops, and More

編集を必要でのように、この記事がマークされます。それの編集により古写本を支援することができます。

原文This article is marked as in need of editing. You can help Codex by editing it.

Sponsored Link