WordPress: The Loop:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
无编辑摘要
 
(未显示2个用户的3个中间版本)
第1行: 第1行:
<span style="border:1px solid #000; text-align:center; float:right; padding:6px;"><strong>导航:</strong> [[WordPress:WordPress文档|上一级]] | [[WordPress]] | {{Template:WordPress导航}}</span>
<div style="clear:both;"></div>
__TOC__
__TOC__
WordPress使用'''Loop'''来显示你的每一篇文章。使用Loop,WordPress将每篇文章显示在当前的网页上,并且根据它们怎样匹配Loop标签内部规定的标准来规定它们的形式。Loop中放置的任何的[[WordPress:Glossary#HTML|HTML]]或者[[WordPress:Glossary#PHP|PHP]]代码会在每篇文章中重现。当WordPress文件申明"这个标签必须在 Loop中",例如特别的[[WordPress:Template Tags|模板标签]]或者插件,标签对于每篇文章都会重复。
WordPress使用'''Loop'''来显示你的每一篇文章。使用Loop,WordPress将每篇文章显示在当前的网页上,并且根据它们怎样匹配Loop标签内部规定的标准来规定它们的形式。Loop中放置的任何的[[WordPress:Glossary#HTML|HTML]]或者[[WordPress:Glossary#PHP|PHP]]代码会在每篇文章中重现。当WordPress文件申明"这个标签必须在 Loop中",例如特别的[[WordPress:Template Tags|模板标签]]或者插件,标签对于每篇文章都会重复。
第8行: 第10行:
对于一个新手,看看Loop,请看[[WordPress:The Loop in Action|活动的Loop]]。
对于一个新手,看看Loop,请看[[WordPress:The Loop in Action|活动的Loop]]。
== 使用 Loop ==
== 使用 Loop ==
The Loop should be placed in <tt>index.php</tt> and in any other Templates used to display post information, but looks slightly different depending on your version of WordPress.  You should first "[[WordPress:WordPress_Versions#Finding_your_version|find what version of WordPress you have]]".


Loop应该被放在<tt>index.php</tt>和其它的任何惯于显示文章信息的模板中,但是根据你使用WordPress版本的不同,Loop看起来可能还会有所不同。你应该首先看看"[[WordPress:WordPress_Versions#Finding_your_version|你拥有的WordPress是哪一个版本]]"。
Loop应该被放在<tt>index.php</tt>和其它的任何惯于显示文章信息的模板中,但是根据你使用WordPress版本的不同,Loop看起来可能还会有所不同。你应该首先看看"[[WordPress:WordPress_Versions#Finding_your_version|你拥有的WordPress是哪一个版本]]"。


=== WordPress 2.0 ===
=== WordPress 2.0 ===
 
这(可能)与WordPress 1.5是相同的
=== WordPress 2.0 ===
 
This is (probably) the same as WordPress 1.5
这(肯能)与WordPress 1.5是相同的
 
=== WordPress 1.5 ===
 
=== WordPress 1.5 ===
=== WordPress 1.5 ===
Be sure to include the call for the header Template at the top of your [[WordPress:Theme Development|Theme]]'s Templates.  If you are using The Loop inside your own design (and your own design is not a template), set <tt>WP_USE_THEMES</tt> to <tt>false</tt>.


要确定包含了对于你的[[WordPress:Theme Development|主题]]的模板的顶上方的页眉模板的访问。如果你在自己的设计内部使用了Loop(而且你自己的设计不是一个模板),将<tt>WP_USE_THEMES</tt>设置为<tt>false</tt>。
要确定包含了对于你的[[WordPress:Theme Development|主题]]的模板的顶上方的页眉模板的访问。如果你在自己的设计内部使用了Loop(而且你自己的设计不是一个模板),将<tt>WP_USE_THEMES</tt>设置为<tt>false</tt>。
<pre><?php define('WP_USE_THEMES', false); get_header(); ?></pre>
<pre><?php define('WP_USE_THEMES', false); get_header(); ?></pre>
<pre><?php define('WP_USE_THEMES', false); get_header(); ?></pre>
The loop starts here:


Loop从这儿开始::
Loop从这儿开始::
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


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


而且从这儿结束:
而且从这儿结束:
  <?php endwhile; else: ?>
  <?php endwhile; else: ?>
  &lt;p&gt;<?php _e('Sorry, no posts matched your criteria.'); ?>&lt;/p&gt;
  &lt;p&gt;<?php _e('Sorry, no posts matched your criteria.'); ?>&lt;/p&gt;
  <?php endif; ?>
  <?php endif; ?>
<?php endwhile; else: ?>
&lt;p&gt;<?php _e('Sorry, no posts matched your criteria.'); ?>&lt;/p&gt;
<?php endif; ?>
=== WordPress 1.2 ===


=== WordPress 1.2 ===
=== WordPress 1.2 ===
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:


要确定在你的索引页的上方包含了对于wp-blog-header.php的访问。记住,wp-blog-header.php的路径必须设置到你的wp-blog-header.php文件的位置:
要确定在你的索引页的上方包含了对于wp-blog-header.php的访问。记住,wp-blog-header.php的路径必须设置到你的wp-blog-header.php文件的位置:
<?php /* Don't remove this line. */ require('./wp-blog-header.php'); ?>


<?php /* 不要移除这一行。 */ require('./wp-blog-header.php'); ?>
<?php /* 不要移除这一行。 */ require('./wp-blog-header.php'); ?>
The loop starts here:


Loop从这儿开始:
Loop从这儿开始:
<?php if ( $posts ) : foreach ( $posts as $post ) : start_wp(); ?>


<?php if ( $posts ) : foreach ( $posts as $post ) : start_wp(); ?>
<?php if ( $posts ) : foreach ( $posts as $post ) : start_wp(); ?>
and ends here:
从这儿结束:
从这儿结束:
<?php endforeach; else: ?>
&lt;p&gt;<?php _e('Sorry, no posts matched your criteria.'); ?>&lt;/p&gt;
<?php endif; ?>


<?php endforeach; else: ?>
<?php endforeach; else: ?>
  &lt;p&gt;<?php _e('对不起,没有文章匹配你的标准。'); ?>&lt;/p&gt;  
  &lt;p&gt;<?php _e('对不起,没有文章匹配你的标准。'); ?>&lt;/p&gt;  
  <?php endif; ?>
  <?php endif; ?>
== Loop Examples ==


== Loop例子 ==
== Loop例子 ==
=== Style Posts From Some Category Differently ===
=== 从一些类别中不同地设计文章===
=== 从一些类别中不同地设计文章===
''For WordPress v1.5 Only''
''只针对 WordPress v1.5''
''只针对 WordPress v1.5''
这个例子,使用[[#WordPress:WordPress1.5 针对1.5版本的语法]],显示了每篇文章和文章标题(用作对于文章的[[WordPress:Using Permalinks|Permalink]]的一个链接),类别和内容。这是一个简单的,梗概的例子;或许你的模板会稍微地显示更多的信息,使设计[[WordPress:Glossary#CSS|CSS]]变得更加地简单。


This example, using the [[WordPress:#WordPress 1.5|syntax for version 1.5]], displays each post with its Title (which is used as a link to the Post's [[WordPress:Using Permalinks|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 [[WordPress:Glossary#CSS|CSS]].
这个例子,使用[[WordPress:#WordPress 1.5针对1.5版本的语法]],显示了每篇文章和文章标题(用作对于文章的[[WordPress:Using Permalinks|Permalink]]的一个链接),类别和内容。这是一个简单的,梗概的例子;或许你的模板会稍微地显示更多的信息,使设计[[WordPress:Glossary#CSS|CSS]]变得更加地简单。
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 <tt>[[WordPress:Template Tags/in_category|in_category()]]</tt> [[WordPress:Template Tags|Template Tag]] is used.
虽然为了更具有一些教育性,但是这个例子允许类别中类别ID'3'的文章,设计得不同。要实现这一点,可以使用<tt>[[WordPress:Template Tags/in_category|在类别中()]]</tt> [[WordPress:Template Tags|模板标签]]。
虽然为了更具有一些教育性,但是这个例子允许类别中类别ID'3'的文章,设计得不同。要实现这一点,可以使用<tt>[[WordPress:Template Tags/in_category|在类别中()]]</tt> [[WordPress:Template Tags|模板标签]]。
The <tt><nowiki><!-- --></nowiki></tt> 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.


<tt><nowiki><!-- --></nowiki></tt>标签是HTML评论标签;如果你使用这个例子,这些标签就不会在网络浏览器中显示。它们除了注释下面的代码以外,什么作用也不起。
<tt><nowiki><!-- --></nowiki></tt>标签是HTML评论标签;如果你使用这个例子,这些标签就不会在网络浏览器中显示。它们除了注释下面的代码以外,什么作用也不起。
<nowiki>
<!-- Start the Loop. -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<nowiki>
<nowiki>
  <!-- Start the Loop. -->
  <!-- Start the Loop. -->
  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?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". -->


<!—下面的内容测试了当前的文章是否在类别3中。-->
<!—下面的内容测试了当前的文章是否在类别3中。-->
  <!—如果是的话, div 框被赋予了 CSS级别 "post-cat-three". -->
  <!—如果是的话, div 框被赋予了 CSS级别 "post-cat-three". -->
  <!—否则的话, div 框 就会被赋予 CSS 级别 "post". -->
  <!—否则的话, div 框 就会被赋予 CSS 级别 "post". -->
<?php if ( in_category('3') ) { ?>
          <div class="post-cat-three">
<?php } else { ?>
          <div class="post">
<?php } ?>


<?php if ( in_category('3') ) { ?>
<?php if ( in_category('3') ) { ?>
第143行: 第66行:
  <?php } ?>
  <?php } ?>


  <!-- Display the Title as a link to the Post's permalink. -->
  <!—显示标题为一个对于文章的permalink的链接。-->
  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
  <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<!—显示标题为一个对于文章的permalink的链接。-->
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<!-- Display the Time. -->
<small><?php the_time('F jS, Y'); ?></small>


<!—显示时间。 -->
<!—显示时间。 -->
  <small><?php the_time('F jS, Y'); ?></small>
  <small><?php the_time('F jS, Y'); ?></small>
<!-- Display the Post's Content in a div box. -->
<div class="entry">
  <?php the_content(); ?>
</div>


<!—在一个div框中显示文章的内容。-->
<!—在一个div框中显示文章的内容。-->
第166行: 第76行:
   <?php the_content(); ?>
   <?php the_content(); ?>
  </div>
  </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 -->
<!—显示文章类别的一个以逗号分开的列表。-->
<!—显示文章类别的一个以逗号分开的列表。-->
  <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
  <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
  </div> <!-- closes the first div box -->
  </div> <!-- closes the first div box -->
<!-- Stop The Loop (but note the "else:" - see next line). -->
<?php endwhile; else: ?>


<!—停止 Loop (但是注意 "else:" – 看下一行). -->
<!—停止 Loop (但是注意 "else:" – 看下一行). -->
  <?php endwhile; else: ?>
  <?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>


<!—第一个"如果" 测试看看有没有文章到 -->
<!—第一个"如果" 测试看看有没有文章到 -->
  <!—显示。这个 "其它的"部分说明了,如果没有文章的话,要做什么。 -->
  <!—显示。这个 "其它的"部分说明了,如果没有文章的话,要做什么。 -->
  <p>对不起,没有文章能够符合你的标准。</p>
  <p>对不起,没有文章能够符合你的标准。</p>
<!-- REALLY stop The Loop. -->
<?php endif; ?></nowiki>


<!—真的吗 停止 Loop. -->
<!—真的吗 停止 Loop. -->
  <?php endif; ?></nowiki>
  <?php endif; ?></nowiki>
'''Note:''' Anytime you want to use [[WordPress:Glossary#HTML|HTML]] code, you ''must'' be outside the <tt><?php &nbsp;?></tt> tags.  [[WordPress:Glossary#PHP|PHP]] code (even things as simple as curly braces: <tt>}</tt> ) ''must'' be inside the <tt><?php &nbsp;?></tt> tags.  You can start and stop the PHP code in order to intersperse HTML code even within <tt>if</tt> and <tt>else</tt> statements, as shown in the above example.


'''注:'''任何你想使用[[WordPress:Glossary#HTML|HTML]]代码的时候,你''必须''在<tt><?php &nbsp;?></tt>标签的外面。[[WordPress:Glossary#PHP|PHP]]代码(甚至向花括号这样简单的东西:<tt>}</tt> )''必须''在<tt><?php &nbsp;?></tt>标签内部。就如以上的例子所显示的那样,你可以开始并结束PHP代码,甚至在<tt>if</tt> 和 <tt>else</tt>声明中散布HTML代码。
'''注:'''任何你想使用[[WordPress:Glossary#HTML|HTML]]代码的时候,你''必须''在<tt><?php &nbsp;?></tt>标签的外面。[[WordPress:Glossary#PHP|PHP]]代码(甚至向花括号这样简单的东西:<tt>}</tt> )''必须''在<tt><?php &nbsp;?></tt>标签内部。就如以上的例子所显示的那样,你可以开始并结束PHP代码,甚至在<tt>if</tt> 和 <tt>else</tt>声明中散布HTML代码。
=== Exclude Posts From Some Category ===


=== 从某个类别中清除文章 ===
=== 从某个类别中清除文章 ===
''For WordPress v1.5 and higher''


''从WordPress v1.5 和更高的版本''
''从WordPress v1.5 和更高的版本''


This example can be used to exclude a certain Category from being displayed.  It is based on the example [[WordPress:#Style Posts From Some Category Differently|above]].
这个例子可以用来,清除某个显示的类别。这个以例子[[#WordPress:Style Posts From Some Category Differently|以上的]]为基础。
 
这个例子可以用来,清除某个显示的类别。这个以例子[[WordPress:#Style Posts From Some Category Differently|以上的]]为基础。


  <nowiki>
  <nowiki>
  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<nowiki>
<?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; ?>


<!—如果文章在我们想要删除的类别中,我们只要转向下一篇文章。-->
<!—如果文章在我们想要删除的类别中,我们只要转向下一篇文章。-->
  <?php if (in_category('3')) continue; ?>
  <?php if (in_category('3')) continue; ?>
<div class="post">


<div class="post">
<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>


<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
第254行: 第114行:
   </div>
   </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; ?></nowiki>
   <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
   <p class="postmetadata">Posted in <?php the_category(', '); ?></p>
  </div> <!-- closes the first div box -->
  </div> <!-- closes the first div box -->
第271行: 第120行:
  <p>对不起,没有文章符合你的标准。</p>
  <p>对不起,没有文章符合你的标准。</p>
  <?php endif; ?></nowiki>
  <?php endif; ?></nowiki>
'''Note''': If you use this example for your main page, you should use a different [[WordPress:Templates|Template]] for your Category archives.  Otherwise, WordPress will exclude all posts in Category 3 even when viewing that Category Archive!


'''注''': 如果你对主页使用这个例子,你应该为你的类别归档使用一个不同的[[WordPress:Templates|模板]]。否则的话,甚至在浏览类别归档时,WordPress会删除类别3中的所有文章,
'''注''': 如果你对主页使用这个例子,你应该为你的类别归档使用一个不同的[[WordPress:Templates|模板]]。否则的话,甚至在浏览类别归档时,WordPress会删除类别3中的所有文章,
However, if you want to use the same template file, you can avoid this by using the <tt>is_home()</tt> tag:


然而,如果你想要使用同一个模板文件的话,你可以使用<tt>is_home()</tt>标签来避免这个:
然而,如果你想要使用同一个模板文件的话,你可以使用<tt>is_home()</tt>标签来避免这个:
...
<?php if (in_category('3') && is_home() ) continue; ?>
...


...
...
  <?php if (in_category('3') && is_home() ) continue; ?>
  <?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 [[WordPress: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.


这个会确保类别3中的文章,只会在主页中清除。也有其它的[[WordPress:Conditional Tags|条件式标签]]可以用来控制输出,取决于关于一个访问的网页,一个特别的条件是不是真的。
这个会确保类别3中的文章,只会在主页中清除。也有其它的[[WordPress:Conditional Tags|条件式标签]]可以用来控制输出,取决于关于一个访问的网页,一个特别的条件是不是真的。


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 [[WordPress:FAQ Layout and Design|Layout and Design FAQ]] or you can use [[WordPress:Template_Tags/query_posts | query_posts]] if you only need to exclude one category from the loop.
请注意即使文章并没有显示,它仍然会被WordPress计算为正在显示的—这意味着,如果你将WordPress设置为显示最多七篇文章,而且七篇中最后的两篇是属于类别3中的,那么你在主页上只会显示五篇文章。如果对于你来说,这是个麻烦事,你可以使用[[WordPress:FAQ Layout and Design|布局和设计 常见问题解答]]上更加复杂的插件,如果你只需要在loop上删除一个类别的话,你可以使用[[WordPress:Template_Tags/query_posts | 查询 文章]]
 
请注意即使文章并没有显示,它仍然会被WordPress计算为正在显示的—这意味着,如果你将WordPress设置为显示最多七篇文章,而且七篇中最后的两篇是属于类别3中的,那么你在主页上只会显示五篇文章。如果对于你来说,这是个麻烦事,你可以使用[[WordPress:FAQ Layout and Design|布局和设计 常见问题解答]]上更加复杂的插件,如果你只需要在loop上删除一个类别的话,你可以使用[[WordPress:Template_Tags/query_posts | 询问 文章]]
 
==Multiple Loops==
 


==多个 Loop==
==多个 Loop==
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.


这个部分处理的是Loop的高级使用。这有一定的技术性- 但不要因为这个而恐慌。我们会从简单的地方开始,并且逐步向难的方向发展。有一点的常识,耐心,和热情,你也可以使用多个loops。
这个部分处理的是Loop的高级使用。这有一定的技术性- 但不要因为这个而恐慌。我们会从简单的地方开始,并且逐步向难的方向发展。有一点的常识,耐心,和热情,你也可以使用多个loops。


First off, <b>"why would one want to use multiple loops?"</b>  In general, the answer is that you might want to do <i>something</i> with one group of posts, and do <i>something different</i> to another group of posts, <u>but display both groups on the same page</u>.  Something could mean almost anything; you are only limited by your PHP skill and your imagination. 
首先,<b>"为什么要使用多个loops?"</b>一般来说,答案是你可能对一组文章想<i>这样</i>处理,对另一组文章想<i>那样</i>处理,<u>但要在同一个网页上显示这两篇文章</u>。那样,可能并不意味着什么,你只是受你的PHP技术和你的想象力的限制。
 
We will get into examples below, but first you should read about the basics.  Take a look at the basic Loop.  It consists of:
 
立马,<b>"为什么要使用多个loops?"</b>一般来说,答案是你可能对一组文章想<i>这样</i>处理,对另一组文章想<i>那样</i>处理,<u>但要在同一个网页上显示这两篇文章</u>。这样,那样,可能并不意味着什么,你只是受你的PHP技术和你的想象力的限制。


我们可能会进入上面的例子,但是首先你应该阅读基本知识。看看基本的Loop。它的组成是:
我们可能会进入上面的例子,但是首先你应该阅读基本知识。看看基本的Loop。它的组成是:
第323行: 第144行:
                 <?php while (have_posts()) : the_post(); ?>     
                 <?php while (have_posts()) : the_post(); ?>     
       &lt;!-- do stuff ... --&gt;
       &lt;!-- do stuff ... --&gt;
       <?php endwhile; ?>
       <?php endwhile; ?>   
 
 
<?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>   
      &lt;!-- do stuff ... --&gt;
      <?php endwhile; ?>
 
 
 
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 <tt>&lt;!-- do stuff ... --&gt;</tt>.  When you hit the last post, stop.  The <tt>do stuff</tt> line(s), are template dependent. 
 
上述的会在英文中阅读(PHP形式和人们熟悉代码表述的可以跳到下面):如果我们要显示文章,就一次一个。对于列表中的每一篇文章,根据<tt>&lt;!-- do stuff ... --&gt;</tt>显示它。当你点击最后一篇文章时,就停止。<tt>do stuff</tt> line(s),是随模板而定的。
 
Aside on <tt>Do stuff</tt>:  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 <tt>do stuff</tt> section would be everything below:
 
在旁边的<tt>Do stuff</tt>上:在这个例子中,在这个例子中,它只是一些怎样规定网页上文章的形式和显示的代码的占位符。根据你希望你的WordPress站点是怎样显示的,这个代码会更改。如果你看了Kubrick主题的 index.php the <tt>do stuff</tt>部分,会有一下的东西:
 
      <?php while (have_posts()) : the_post(); ?>
To above:
 
      <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>


上述的会在英文中阅读(PHP形式和人们熟悉代码表述的可以跳到下面):如果我们要显示文章,就一次一篇。对于列表中的每一篇文章,根据<tt>&lt;!-- do stuff ... --&gt;</tt>显示它。当你点击最后一篇文章时,就停止。<tt>do stuff</tt> line(s),是随模板而定的。


在旁边的<tt>Do stuff</tt>上:在这个例子中,它只是一些怎样规定网页上文章的形式和显示的代码的占位符。根据你希望你的WordPress站点是怎样显示的,这个代码会更改。如果你看了Kubrick主题的 index.php the <tt>do stuff</tt>部分,会有以下的东西:
<?php while (have_posts()) : the_post(); ?>
<?php while (have_posts()) : the_post(); ?>
上面的:
上面的:
第352行: 第154行:
       <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>
       <?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?>


An explanation for the coders out there:
编码器在那儿的一个解释:
编码器在那儿的一个解释:
The <tt>have_posts()</tt> and <tt>the_post()</tt> are convenience wrappers around the global <tt>$wp_query</tt> object, which is where all of the action is.  The <tt>$wp_query</tt> is called in the blog header and fed query arguments coming in through <tt>GET</tt> and <tt>PATH_INFO</tt>.  The <tt>$wp_query</tt> 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 <tt>$posts</tt> array (for backward compatibility with old post loops).


<tt>have_posts()</tt>和<tt>the_post()</tt>是全局的<tt>$wp_query</tt>物体有用的包装材料,这是所有活动的地点。可以在博客页眉中访问<tt>$wp_query</tt>。你可以博客页眉中访问<tt>$wp_query</tt> 而且以来自<tt>GET</tt> and <tt>PATH_INFO</tt>的询问论据为支撑。<tt>$wp_query</tt>接受争论,并且建立和执行一个以一批文章为结果的DB询问。这个数组在一个对象中储存,而且也会返回到博客页眉中,在这里,
<tt>have_posts()</tt>和<tt>the_post()</tt>是全局的<tt>$wp_query</tt>物体有用的包装材料,这是所有活动的地点。可以在博客页眉中访问<tt>$wp_query</tt>。你可以博客页眉中访问<tt>$wp_query</tt> 而且以来自<tt>GET</tt> and <tt>PATH_INFO</tt>的询问论据为支撑。<tt>$wp_query</tt>接受争论,并且建立和执行一个以一批文章为结果的DB询问。这个数组在一个对象中储存,而且也会返回到博客页眉中,在这里,
它塞满了全局<tt>$posts</tt>数组(关于旧的文章loops的向后兼容)。
它塞满了全局<tt>$posts</tt>数组(关于旧的文章loops的向后兼容)。


Once WordPress has finished loading the blog header and is descending into the template, we arrive at our post Loop.  The <tt>have_posts()</tt> simply calls into <tt>$wp_query->have_posts()</tt> which checks a loop counter to see if there are any posts left in the post array.  And <tt>the_post()</tt> calls <tt>$wp_query->the_post()</tt> which advances the loop counter and sets up the global <tt>$post</tt> variable as well as all of the global post data.  Once we have exhausted the loop, <tt>have_posts()</tt> will return false and we are done.
一旦WordPress停止了载入博客页眉,并且下降到模板中,我们便到达了我们的文章Loop上。<tt>have_posts()</tt>只是访问到<tt>$wp_query->have_posts()</tt>,这个检查loop计数器,来看看在文字排列中有没有剩下任何文章。而且<tt>the_post()</tt>访问<tt>$wp_query->the_post()</tt>,会升级loop计数器,和设置全局的<tt>$post</tt>变数,以及所有的全球的文章数据。我们一旦耗尽了loop, <tt>have_posts()</tt>就会返回错误,我们完成了。
 
一旦WordPress停止了载入博客页眉,并且下降到模板中,我们便到达了我们的文章Loop上。<tt>have_posts()</tt>只是访问到<tt>$wp_query->have_posts()</tt>,这个检查loop计算器,来看看在文字排列中有没有剩下任何文章。而且<tt>the_post()</tt>访问<tt>$wp_query->the_post()</tt>,会升级loop计算器,和设置全局的<tt>$post</tt>变数,以及所有的全球的文章数据。我们一旦耗尽了loop, <tt>have_posts()</tt>就会返回错误,我们完成了。
 
==== Loop Examples====


==== Loop 例子====
==== Loop 例子====
 
下面是使用多个loops的两个例子。使用多个loops的关键是<tt>$wp_query</tt>只能被访问一次。要传播这个,通过访问<tt>rewind_posts()</tt> 或者通过创建一个新的查询对象,有可能再次地使用查询。这个在例1中说明了。在例2中,包含用一个变数来储存一个查询的结果。例3说明了使用<tt>update_post_caches();</tt>功能来避免常见的插件问题。最后,‘多个运转的 loops’将许多的观点集中在一起,来一方面说明使用多个loop来提升你的博客主页上的某个类别中的文章。
Below are two examples of using multiple loops.  The key to using multiple loops is that <tt>$wp_query</tt> can only be called once.  In order to get around this it is possible to re-use the query by calling <tt>rewind_posts()</tt> 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 <tt>update_post_caches();</tt> 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.
 
下面是使用多个loops的两个例子。使用多个loops的关键是<tt>$wp_query</tt>只能被访问一次。要传播这个,通过访问<tt>rewind_posts()</tt> 或者通过创建一个新的询问对象,有可能再次地使用询问。这个在例一中说明了。在例2中,包含用一个变数来储存一个询问的结果。例3说明了使用<tt>update_post_caches();</tt>功能来避免常见的插件问题。最后,‘多个运转的 loops’将许多的观点集中在一起,来一方面说明使用多个loop来提升你的博客主页上的某个类别中的文章。
 
=====Multiple Loops Example 1=====


=====多个 Loops 例子 1=====
=====多个 Loops 例子 1=====
In order to loop through the same query a second time, call <tt>rewind_posts()</tt>.  This will reset the loop counter and allow you to do another loop. 


为了第二次在同一个询问中loop,访问<tt>rewind_posts()</tt>。这样就会重新设置loop计算器,允许你做另一个loop。
为了第二次在同一个询问中loop,访问<tt>rewind_posts()</tt>。这样就会重新设置loop计算器,允许你做另一个loop。
  <?php rewind_posts(); ?>
 
  <?php while (have_posts()) : the_post(); ?>
    &lt;!-- Do stuff... --&gt;
  <?php endwhile; ?>
<?php rewind_posts(); ?>
<?php rewind_posts(); ?>
    
    
第395行: 第173行:
   <?php endwhile; ?>
   <?php endwhile; ?>


如果你完成了最初的查询中的文章,你想要使用一个不同的查询,你可以通过访问<tt>query_posts()</tt>再次地使用<tt>$wp_query</tt>对象,然后''looping back''。<tt>query_posts()</tt>会执行一个新的查询,创建一个新的文章列,并且重新设置loop计算器。


 
  // 在一个特别的_cat 类别中得到最后的十篇文章。
If you are finished with the posts in the original query, and you want to use a different query, you can reuse the <tt>$wp_query</tt> object by calling <tt>query_posts()</tt> and then ''looping back'' through.  The <tt>query_posts()</tt> will perform a new query, build a new posts array, and reset the loop counter.
 
如果你完成了最初的询问中的文章,你想要使用一个不同的询问,你可以通过访问<tt>query_posts()</tt>再次地使用<tt>$wp_query</tt>对象,然后''looping back''。<tt>query_posts()</tt>会执行一个新的询问,创建一个新的文章列,并且重新设置loop计算器。
 
  // 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(); ?>
    &lt;!-- Do special_cat stuff... --&gt;
  <?php endwhile;?>
 
 
 
// 在一个特别的_cat 类别中得到最后的十篇文章。
   <?php query_posts('category_name=special_cat&showposts=10'); ?>
   <?php query_posts('category_name=special_cat&showposts=10'); ?>
   
   
第417行: 第182行:
   <?php endwhile;?>
   <?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(); ?>
  &lt;!-- Do special_cat stuff... --&gt;
<?php endwhile; ?>
 
 
 
<?php $my_query = new WP_Query('category_name=special_cat&showposts=10'); ?>
<?php $my_query = new WP_Query('category_name=special_cat&showposts=10'); ?>
   
   
第435行: 第189行:
  <?php endwhile; ?>
  <?php endwhile; ?>


 
使用了查询对象<tt>my_query</tt>因为你不能使用全局的<tt>have_posts()</tt> <tt>the_post()</tt>因为它们都使用<tt>$wp_query</tt>。
 
The query object <tt>my_query</tt> is used because you cannot use the global <tt>have_posts()</tt> and <tt>the_post()</tt> since they both use <tt>$wp_query</tt>.  Instead, call into your new <tt>$my_query</tt> object.
 
使用了询问对象<tt>my_query</tt>因为你不能使用全局的<tt>have_posts()</tt> and <tt>the_post()</tt>因为它们都使用<tt>$wp_query</tt>。
 
=====Multiple Loops Example 2=====


=====多个Loops 例2=====
=====多个Loops 例2=====


Another version of using multiple Loops takes another tack for getting around the inability to use <tt>have_posts()</tt> and <tt>the_post()</tt>.  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.
使用多个Loops的另一个版本,对于不能使用<tt>have_posts()</tt> 和 <tt>the_post()</tt>采取了另一个方针。为了解决这个问题,你应该在一个变数中储存原始查询,当针对另一个Loop时,需要重新地提交查询。这样做,你可以使用所有依赖于全球性的标准功能。
 
使用多个Loops的另一个版本,对于不能使用<tt>have_posts()</tt> 和 <tt>the_post()</tt>采取了另一个方针。为了解决这个问题,你应该在一个变数中储存原始询问,当针对另一个Loop时,需要重新地上交询问。这样做,你可以使用所有依赖于全球性的标准功能。
 
For example:


例如:
例如:
// going off on my own here
// 独自从这儿离开
// 独自从这儿离开
  <?php $temp_query = $wp_query; ?>
  <?php $temp_query = $wp_query; ?>
&lt;!-- Do stuff... --&gt;
<?php query_posts('category_name=special_cat&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
  &lt;!-- Do special_cat stuff... --&gt;
<?php endwhile; ?>
// now back to our regularly scheduled programming
<?php $wp_query = $temp_query; ?>
<?php $temp_query = $wp_query; ?>
  &lt;!-- Do stuff... --&gt;
  &lt;!-- Do stuff... --&gt;
   
   
第478行: 第207行:
   
   
  // 现在返回到我们有规律的规划<?php $wp_query = $temp_query; ?>
  // 现在返回到我们有规律的规划<?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:


'''注:''' 在 PHP 5中,对象以操作员为参考,而不是向在php4中那样的复制的。使例2能在PHP5中运行,你需要使用以下的代码:
'''注:''' 在 PHP 5中,对象以操作员为参考,而不是向在php4中那样的复制的。使例2能在PHP5中运行,你需要使用以下的代码:
<pre>
// going off on my own here
<?php $temp_query = clone $wp_query; ?>
&lt;!-- Do stuff... --&gt;
<?php query_posts('category_name=special_cat&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
  &lt;!-- Do special_cat stuff... --&gt;
<?php endwhile; ?>
// now back to our regularly scheduled programming
<?php $wp_query = clone $temp_query; ?>
</pre>




第519行: 第226行:
</pre>
</pre>


However, this second example does not work in WordPress 2.1.
然而,第二个例子不能在WordPress 2.1中运行。
然而,第二个例子不能在WordPress 2.1中运行。
=====Multiple Loops Example 3 - Plugins=====


=====多个 Loops 例 3 – 插件=====
=====多个 Loops 例 3 – 插件=====
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 <tt>update_post_caches($posts)</tt> 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.


经发现许多的插件并不能与多个loops合作。在这些情况下,经发现,许多的处理文章的关键词和标签的插件,只对文章第一个包含全部文章的子集的一个loop起作用。如果你发现这是一个问题,你可能想要尝试一下添加了<tt>update_post_caches($posts)</tt>功能的基本的loop在以下的执行情况。这个功能重新设置了文章高速缓存,而且至今没有被证明。只有第一个loop重新得到一个文章的子集,才会执行第二个loop。
经发现许多的插件并不能与多个loops合作。在这些情况下,经发现,许多的处理文章的关键词和标签的插件,只对文章第一个包含全部文章的子集的一个loop起作用。如果你发现这是一个问题,你可能想要尝试一下添加了<tt>update_post_caches($posts)</tt>功能的基本的loop在以下的执行情况。这个功能重新设置了文章高速缓存,而且至今没有被证明。只有第一个loop重新得到一个文章的子集,才会执行第二个loop。
Simply amend:


仅仅修改:
仅仅修改:
     <?php if (have_posts()) : ?>
     <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>   
      &lt;!-- Do stuff... --&gt;
    <?php endwhile; ?>
to become:
    <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); update_post_caches($posts); ?>
      &lt;!-- Do stuff... --&gt;
    <?php endwhile; ?>
<?php if (have_posts()) : ?>
                 <?php while (have_posts()) : the_post(); ?>     
                 <?php while (have_posts()) : the_post(); ?>     
       &lt;!-- Do stuff... --&gt;
       &lt;!-- Do stuff... --&gt;
第564行: 第244行:
       &lt;!-- Do stuff... --&gt;
       &lt;!-- Do stuff... --&gt;
     <?php endwhile; ?>
     <?php endwhile; ?>
====Multiple Loops in Action====


====运转的多个Loops ====
====运转的多个Loops ====
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.
了解怎样使用多个loops的最好的方法,就是显示一个多个loops的使用的一个例子。也许多个loops最常用的就是在一个网页上显示两个(或者更多的)文章列表。当一个WEB站点管理员不仅想要显示最近所写的文章,还想要显示属于某一类别的文章的时候,便要用到这个。
了解怎样使用多个loops的最好的方法,就是显示一个多个loops的使用的一个例子。也许多个loops最常用的就是在一个网页上显示两个(或者更多的)文章列表。当一个WEB站点管理员不仅想要显示最近所写的文章,还想要显示属于某一类别的文章的时候,便要用到这个。
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).  <b>The catch is that no post should appear in both categories.</b>


将所有的形式和CSS问题放到一边,假如我们只想要文章的两个列表。一个会列出最近的文章(最近添加的10个标准的文章),另一个列表只会包含一个来自类别‘特色的’文章。属于‘特色的’类别里的文章应该首先显示,紧跟着,第二个文章的列表(标准的)。<b> 所获得的是没有一篇文章同时出现在两个类别中。</b>
将所有的形式和CSS问题放到一边,假如我们只想要文章的两个列表。一个会列出最近的文章(最近添加的10个标准的文章),另一个列表只会包含一个来自类别‘特色的’文章。属于‘特色的’类别里的文章应该首先显示,紧跟着,第二个文章的列表(标准的)。<b> 所获得的是没有一篇文章同时出现在两个类别中。</b>


<b>Step 1.</b>  Get only one post from the ‘featured’ category.
<b>第一步.</b>  从‘有特色的’类中,只拿出一篇文章。
 
<b>第一步.</b>  从‘有特色的’类被中,只拿出一篇文章。
   <?php $my_query = new WP_Query('category_name=featured&showposts=1');
   <?php $my_query = new WP_Query('category_name=featured&showposts=1');
   while ($my_query->have_posts()) : $my_query->the_post();
   while ($my_query->have_posts()) : $my_query->the_post();
第587行: 第256行:
     &lt;!-- Do stuff... --&gt;
     &lt;!-- Do stuff... --&gt;
   <?php endwhile; ?>
   <?php endwhile; ?>
<?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; ?>
    &lt;!-- Do stuff... --&gt;
  <?php endwhile; ?>
In English the above code would read:
在英文中,上述的代码会读作:
在英文中,上述的代码会读作:


Set <tt>$my_query</tt> 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 <tt>$do_not_duplicate</tt> equal to the ID number of the single post returned.  Recall that the <tt>Do stuff</tt> line represents all the formatting options associated for the post retrieved.
将<tt>$my_query</tt>设置为与类别是特色的里的所有文章的查询结果相同,顺便说一下,只给我一篇文章。也将变数<tt>$do_not_duplicate</tt>设置为与返回的单个文章的ID数字相等。记起,<tt>Do stuff</tt>行代表了所有的与重新得到的文章相关的格式的选项。  
 
将<tt>$my_query</tt>设置为与类别是特色的里的所有文章的查询结果相同,顺便说一下,只给我一篇文章。也将变数<tt>$do_not_duplicate</tt>设置为与返回的单个文章的ID数字相等。记起,<tt>Do stuff</tt>行代表了所有的与重新得到的文章相关的格式的选项。
 
Note that we will need the value of <tt>$do_not_duplicate</tt> in the next step to ensure that the same post doesn't appear in both lists.   


注意我们需要下一步中的<tt>$do_not_duplicate</tt>值,以确保相同的文章没有在两个类别中重复出现。
注意我们需要下一步中的<tt>$do_not_duplicate</tt>值,以确保相同的文章没有在两个类别中重复出现。
<b>Step 2</b>.  The second loop, get the X latest posts (except one).


<b>第二步</b>.  第二个loop,得到了X篇最新的文章(除了一篇)。
<b>第二步</b>.  第二个loop,得到了X篇最新的文章(除了一篇)。
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 <tt>Do stuff</tt>.


下面的代码得到了最新的X篇文章(如WordPress参数选择中所定义的那样),保存了已经在第一个loop中显示的那一篇,并且根据<tt>Do stuff</tt>显示它们。
下面的代码得到了最新的X篇文章(如WordPress参数选择中所定义的那样),保存了已经在第一个loop中显示的那一篇,并且根据<tt>Do stuff</tt>显示它们。
  <?php if (have_posts()) : while (have_posts()) : the_post();
  if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    &lt;!-- Do stuff... --&gt;
  <?php endwhile; endif; ?>




第628行: 第272行:
   <?php endwhile; endif; ?>
   <?php endwhile; endif; ?>


In English the above code would read:
在英文中,以上的代码会读作:
在英文中,以上的代码会读作:
Get all posts, where a post equals <tt>$do_not_duplicate</tt> then just do nothing (<tt>continue</tt>), otherwise display all the other the posts according to <tt>Do stuff</tt>.  Also, update the cache so the tagging and keyword plugins play nice.  Recall, <tt>$do_not_duplicate</tt> variable contains the ID of the post already displayed.


得到所有的文章,在文章等于<tt>$do_not_duplicate</tt>的位置,什么措施也别采取,否则的话,根据<tt>Do stuff</tt>显示所有的其它的文章。而且,更新高速缓存,这样标签和关键词会运行的很好。记住,<tt>$do_not_duplicate</tt>参数包含了已经显示的文章的ID.
得到所有的文章,在文章等于<tt>$do_not_duplicate</tt>的位置,什么措施也别采取,否则的话,根据<tt>Do stuff</tt>显示所有的其它的文章。而且,更新高速缓存,这样标签和关键词会运行的很好。记住,<tt>$do_not_duplicate</tt>参数包含了已经显示的文章的ID.


<b>The End Result</b>
<b>最后的结果</b>
<b>最后的结果</b>
Here is what the final piece of code looks like <u>without any formatting</u>:


以下是最后的代码是怎样显示的<u>without any formatting</u>:
以下是最后的代码是怎样显示的<u>without any formatting</u>:
第655行: 第290行:
     &lt;!-- Do stuff... --&gt;
     &lt;!-- Do stuff... --&gt;
   <?php endwhile; endif; ?>
   <?php endwhile; endif; ?>
<?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;?>
    &lt;!-- Do stuff... --&gt;
  <?php endwhile; ?>
    &lt;!-- Do other stuff... --&gt;
  <?php if (have_posts()) : while (have_posts()) : the_post();
  if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    &lt;!-- Do stuff... --&gt;
  <?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) <i>except</i> 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 [http://codex.WordPress.org/Template_Hierarchy Template Hierarchy] to create a different look for home.php and index.php.  See associated resources at the bottom of this page.


最后的结果是一篇文章有两个列表。第一个列表只包含了一篇文章—来自'特色'的最近的文章。第二个列表会包含X篇最近的文章(如WordPress参数中所定义的)<i>except</i>已经在第一个列表中显示的文章。因此,一旦特色文章被另一个代替了,前一个特色文章就会在下面的标准的文章列表中显示(这取决于你选择显示多少篇文章和文章的频率)。这种方法(或者相似的)由许多人使用,与[http://codex.WordPress.org/Template_Hierarchy 模板层级]的知识想结合,为home.php 和 index.php创建不同的外观。在这一个页面的底部看看相关的资源。
最后的结果是一篇文章有两个列表。第一个列表只包含了一篇文章—来自'特色'的最近的文章。第二个列表会包含X篇最近的文章(如WordPress参数中所定义的)<i>except</i>已经在第一个列表中显示的文章。因此,一旦特色文章被另一个代替了,前一个特色文章就会在下面的标准的文章列表中显示(这取决于你选择显示多少篇文章和文章的频率)。这种方法(或者相似的)由许多人使用,与[http://codex.WordPress.org/Template_Hierarchy 模板层级]的知识想结合,为home.php 和 index.php创建不同的外观。在这一个页面的底部看看相关的资源。
<h3>Sources:</h3> This article on multiple loops is a combination of [http://boren.nu Ryan Boren] and [http://www.alexking.org Alex King's]
[http://comox.textdrive.com/pipermail/hackers/2005-January/003578.html discussion] about the Loop on the [[WordPress:Mailing Lists#Hackers|Hackers Mailing List]] as well as the tutorial written at [http://www.maxpower.ca/wordPress-hack-sticky-adhesive-kubrick/2005/05/03/ MaxPower].


<h3>原始资料:</h3>这篇关于多个loops的文章是[http://boren.nu Ryan Boren]和 [http://www.alexking.org Alex King's] 的结合体。
<h3>原始资料:</h3>这篇关于多个loops的文章是[http://boren.nu Ryan Boren]和 [http://www.alexking.org Alex King's] 的结合体。
第681行: 第297行:


   
   
==More Loop Resources==
==更多的 Loop 资源==
==更多的 Loop 资源==
To learn more about the WordPress Loop, and the various template tags that work only within the Loop, here are more resources.


学习更多的WordPress Loop, 以及只在Loop内部运行的不同的模板,这儿有一些资源。
学习更多的WordPress Loop, 以及只在Loop内部运行的不同的模板,这儿有一些资源。
* [[WordPress:The Loop in Action]]
* [[WordPress:Template Tags]]
* [[WordPress:Templates|Using the Loop in Template Files]]


* [[WordPress:The Loop in Action|运转的Loop]]
* [[WordPress:The Loop in Action|运转的Loop]]
第699行: 第305行:
* [[WordPress:Templates|在模板文件中使用Loop]]
* [[WordPress:Templates|在模板文件中使用Loop]]


===资源===




===Resources===
===资源===
* [http://mattread.com/archives/2005/04/wordpress-is-not-php/ Matt Read Loop Article]
* [http://mattread.com/archives/2005/04/wordpress-is-not-php/ Matt 阅读Loop 文章]
* [http://mattread.com/archives/2005/04/wordpress-is-not-php/ Matt 阅读Loop 文章]
* [http://www.maxpower.ca/wordPress-hack-sticky-adhesive-kubrick/2005/05/03/ MaxPower Dynamic Sticky Tutorial]
* [http://www.maxpower.ca/wordPress-hack-sticky-adhesive-kubrick/2005/05/03/ MaxPower Dynamic Sticky 指南]
* [http://www.maxpower.ca/wordPress-hack-sticky-adhesive-kubrick/2005/05/03/ MaxPower Dynamic Sticky 指南]
* [http://shaunandrews.com/2006/07/14/wordpress-sticky-posts/ Shaun Andrews' Sticky Posts that disappear after a few days]
* [http://shaunandrews.com/2006/07/14/wordpress-sticky-posts/ Shaun Andrews' Sticky 消失几天后的文章]
* [http://shaunandrews.com/2006/07/14/wordpress-sticky-posts/ Shaun Andrews' Sticky 消失几天后的文章]
* [http://www.ifelse.co.uk/archives/2005/04/08/query_posts-redux/ IfElse Query_post Redux]
* [http://www.ifelse.co.uk/archives/2005/04/08/query_posts-redux/ IfElse Query_post Redux]
* [http://www.ifelse.co.uk/archives/2005/04/08/query_posts-redux/ IfElse Query_post Redux]
* [http://www.optiniche.com/blog/15/the-wordpress-loop-and-adding-content-outside-of-it/ The Loop and Adding Content Outside of It]
* [http://www.optiniche.com/blog/15/the-wordpress-loop-and-adding-content-outside-of-it/ Loop并且在它的外面添加内容]
* [http://www.optiniche.com/blog/15/the-wordpress-loop-and-adding-content-outside-of-it/ Loop并且在它的外面添加内容]
* [http://rhymedcode.net/1001-wordpression-loops/ 1001 WordPression Loops]
* [http://rhymedcode.net/1001-wordpression-loops/ 1001 WordPression Loops]
* [http://rhymedcode.net/1001-wordpression-loops/ 1001 WordPression Loops]
* [http://weblogtoolscollection.com/archives/2007/06/06/global-variables-and-the-wordpress-loop/ Global Variables and the WordPress Loop]
* [http://weblogtoolscollection.com/archives/2007/06/06/global-variables-and-the-wordpress-loop/ 全程变量和 WordPress Loop]
* [http://weblogtoolscollection.com/archives/2007/06/06/global-variables-and-the-wordpress-loop/ 全程变量和 WordPress Loop]
* [http://perishablepress.com/press/2006/11/22/perishable-press-triple-loop-for-wordpress/ WordPress Triple Loop Tutorial]
* [http://perishablepress.com/press/2006/11/22/perishable-press-triple-loop-for-wordpress/ WordPress Triple Loop 指南]
* [http://perishablepress.com/press/2006/11/22/perishable-press-triple-loop-for-wordpress/ WordPress Triple Loop 指南]
* [http://perishablepress.com/press/2007/08/06/super-loop-exclude-specific-categories-and-display-any-number-of-posts/ Super Loop: Exclude Categories and Limit Number of Posts]
* [http://perishablepress.com/press/2007/08/06/super-loop-exclude-specific-categories-and-display-any-number-of-posts/ 超级的 Loop: 清除列表和现在文章篇数]
* [http://perishablepress.com/press/2007/08/06/super-loop-exclude-specific-categories-and-display-any-number-of-posts/ 超级的 Loop: 清除列表和现在文章篇数]
* [http://perishablepress.com/press/2007/11/14/easily-adaptable-wordpress-loop-templates/ Easily Adaptable WordPress Loop Templates: Basic Loops, Mullet Loops, and More]
* [http://perishablepress.com/press/2007/11/14/easily-adaptable-wordpress-loop-templates/ 容易适应的 WordPress Loop 模板: 基本的Loops, Mullet Loops,以及更多的]
* [http://perishablepress.com/press/2007/11/14/easily-adaptable-wordpress-loop-templates/ 容易适应的 WordPress Loop 模板: 基本的Loops, Mullet Loops,以及更多的]




{{Copyedit}}
{{Copyedit}}

2008年5月27日 (二) 10:50的最新版本

导航: 上一级 | WordPress | 首页 | WordPress中文论坛 | WordPress主机 | CMS程序 | 论坛程序 | ECShop | ShopNC | PowerEasy

WordPress使用Loop来显示你的每一篇文章。使用Loop,WordPress将每篇文章显示在当前的网页上,并且根据它们怎样匹配Loop标签内部规定的标准来规定它们的形式。Loop中放置的任何的HTML或者PHP代码会在每篇文章中重现。当WordPress文件申明"这个标签必须在 Loop中",例如特别的模板标签或者插件,标签对于每篇文章都会重复。

例如,在信息中,Loop对于每篇文章的在WordPress1.5中的显示是:标题 (标题()), 时间 (时间()), 和类别 (类别()) 关于每篇文章的其他信息能用适当的标签或者通过$post的变量(为高级使用者的)来展示,当Loop正在运行的时候, $post 变数是由当前文章的信息设置的


对于一个新手,看看Loop,请看活动的Loop

使用 Loop[ ]

Loop应该被放在index.php和其它的任何惯于显示文章信息的模板中,但是根据你使用WordPress版本的不同,Loop看起来可能还会有所不同。你应该首先看看"你拥有的WordPress是哪一个版本"。

WordPress 2.0[ ]

这(可能)与WordPress 1.5是相同的

WordPress 1.5[ ]

要确定包含了对于你的主题的模板的顶上方的页眉模板的访问。如果你在自己的设计内部使用了Loop(而且你自己的设计不是一个模板),将WP_USE_THEMES设置为false

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

Loop从这儿开始::

<?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[ ]

要确定在你的索引页的上方包含了对于wp-blog-header.php的访问。记住,wp-blog-header.php的路径必须设置到你的wp-blog-header.php文件的位置:

<?php /* 不要移除这一行。 */ require('./wp-blog-header.php'); ?>

Loop从这儿开始:

<?php if ( $posts ) : foreach ( $posts as $post ) : start_wp(); ?> 从这儿结束:

<?php endforeach; else: ?>

<p><?php _e('对不起,没有文章匹配你的标准。'); ?></p> 
<?php endif; ?>

Loop例子[ ]

从一些类别中不同地设计文章[ ]

只针对 WordPress v1.5 这个例子,使用#WordPress:WordPress1.5 针对1.5版本的语法,显示了每篇文章和文章标题(用作对于文章的Permalink的一个链接),类别和内容。这是一个简单的,梗概的例子;或许你的模板会稍微地显示更多的信息,使设计CSS变得更加地简单。

虽然为了更具有一些教育性,但是这个例子允许类别中类别ID'3'的文章,设计得不同。要实现这一点,可以使用在类别中() 模板标签

<!-- -->标签是HTML评论标签;如果你使用这个例子,这些标签就不会在网络浏览器中显示。它们除了注释下面的代码以外,什么作用也不起。 <!-- Start the Loop. --> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <!—下面的内容测试了当前的文章是否在类别3中。--> <!—如果是的话, div 框被赋予了 CSS级别 "post-cat-three". --> <!—否则的话, div 框 就会被赋予 CSS 级别 "post". --> <?php if ( in_category('3') ) { ?> <div class="post-cat-three"> <?php } else { ?> <div class="post"> <?php } ?> <!—显示标题为一个对于文章的permalink的链接。--> <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> <!-- closes the first div box --> <!—停止 Loop (但是注意 "else:" – 看下一行). --> <?php endwhile; else: ?> <!—第一个"如果" 测试看看有没有文章到 --> <!—显示。这个 "其它的"部分说明了,如果没有文章的话,要做什么。 --> <p>对不起,没有文章能够符合你的标准。</p> <!—真的吗 停止 Loop. --> <?php endif; ?>

注:任何你想使用HTML代码的时候,你必须<?php  ?>标签的外面。PHP代码(甚至向花括号这样简单的东西:}必须<?php  ?>标签内部。就如以上的例子所显示的那样,你可以开始并结束PHP代码,甚至在ifelse声明中散布HTML代码。

从某个类别中清除文章[ ]

从WordPress v1.5 和更高的版本

这个例子可以用来,清除某个显示的类别。这个以例子以上的为基础。

 <?php if ( have_posts() ) : while ( have_posts() ) : the_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>对不起,没有文章符合你的标准。</p>
 <?php endif; ?>

: 如果你对主页使用这个例子,你应该为你的类别归档使用一个不同的模板。否则的话,甚至在浏览类别归档时,WordPress会删除类别3中的所有文章,

然而,如果你想要使用同一个模板文件的话,你可以使用is_home()标签来避免这个:

...

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

这个会确保类别3中的文章,只会在主页中清除。也有其它的条件式标签可以用来控制输出,取决于关于一个访问的网页,一个特别的条件是不是真的。

请注意即使文章并没有显示,它仍然会被WordPress计算为正在显示的—这意味着,如果你将WordPress设置为显示最多七篇文章,而且七篇中最后的两篇是属于类别3中的,那么你在主页上只会显示五篇文章。如果对于你来说,这是个麻烦事,你可以使用布局和设计 常见问题解答上更加复杂的插件,如果你只需要在loop上删除一个类别的话,你可以使用 查询 文章

多个 Loop[ ]

这个部分处理的是Loop的高级使用。这有一定的技术性- 但不要因为这个而恐慌。我们会从简单的地方开始,并且逐步向难的方向发展。有一点的常识,耐心,和热情,你也可以使用多个loops。

首先,"为什么要使用多个loops?"一般来说,答案是你可能对一组文章想这样处理,对另一组文章想那样处理,但要在同一个网页上显示这两篇文章。那样,可能并不意味着什么,你只是受你的PHP技术和你的想象力的限制。

我们可能会进入上面的例子,但是首先你应该阅读基本知识。看看基本的Loop。它的组成是:

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

上述的会在英文中阅读(PHP形式和人们熟悉代码表述的可以跳到下面):如果我们要显示文章,就一次一篇。对于列表中的每一篇文章,根据<!-- do stuff ... -->显示它。当你点击最后一篇文章时,就停止。do stuff line(s),是随模板而定的。

在旁边的Do stuff上:在这个例子中,它只是一些怎样规定网页上文章的形式和显示的代码的占位符。根据你希望你的WordPress站点是怎样显示的,这个代码会更改。如果你看了Kubrick主题的 index.php the do stuff部分,会有以下的东西: <?php while (have_posts()) : the_post(); ?> 上面的:

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

编码器在那儿的一个解释:

have_posts()the_post()是全局的$wp_query物体有用的包装材料,这是所有活动的地点。可以在博客页眉中访问$wp_query。你可以博客页眉中访问$wp_query 而且以来自GET and PATH_INFO的询问论据为支撑。$wp_query接受争论,并且建立和执行一个以一批文章为结果的DB询问。这个数组在一个对象中储存,而且也会返回到博客页眉中,在这里, 它塞满了全局$posts数组(关于旧的文章loops的向后兼容)。

一旦WordPress停止了载入博客页眉,并且下降到模板中,我们便到达了我们的文章Loop上。have_posts()只是访问到$wp_query->have_posts(),这个检查loop计数器,来看看在文字排列中有没有剩下任何文章。而且the_post()访问$wp_query->the_post(),会升级loop计数器,和设置全局的$post变数,以及所有的全球的文章数据。我们一旦耗尽了loop, have_posts()就会返回错误,我们完成了。

Loop 例子[ ]

下面是使用多个loops的两个例子。使用多个loops的关键是$wp_query只能被访问一次。要传播这个,通过访问rewind_posts() 或者通过创建一个新的查询对象,有可能再次地使用查询。这个在例1中说明了。在例2中,包含用一个变数来储存一个查询的结果。例3说明了使用update_post_caches();功能来避免常见的插件问题。最后,‘多个运转的 loops’将许多的观点集中在一起,来一方面说明使用多个loop来提升你的博客主页上的某个类别中的文章。

多个 Loops 例子 1[ ]

为了第二次在同一个询问中loop,访问rewind_posts()。这样就会重新设置loop计算器,允许你做另一个loop。 <?php rewind_posts(); ?>

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

如果你完成了最初的查询中的文章,你想要使用一个不同的查询,你可以通过访问query_posts()再次地使用$wp_query对象,然后looping backquery_posts()会执行一个新的查询,创建一个新的文章列,并且重新设置loop计算器。

 // 在一个特别的_cat 类别中得到最后的十篇文章。
  <?php query_posts('category_name=special_cat&showposts=10'); ?>

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

如果你需要保存原来的查询,你可以创建一个新的查询对象。 <?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; ?>

使用了查询对象my_query因为你不能使用全局的have_posts()the_post()因为它们都使用$wp_query

多个Loops 例2[ ]

使用多个Loops的另一个版本,对于不能使用have_posts()the_post()采取了另一个方针。为了解决这个问题,你应该在一个变数中储存原始查询,当针对另一个Loop时,需要重新地提交查询。这样做,你可以使用所有依赖于全球性的标准功能。

例如: // 独自从这儿离开

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

// 现在返回到我们有规律的规划<?php $wp_query = $temp_query; ?>

注: 在 PHP 5中,对象以操作员为参考,而不是向在php4中那样的复制的。使例2能在PHP5中运行,你需要使用以下的代码:


 // 自己离开来到这儿
 <?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; ?>
 
 // 现在返回到我们正常规定的项目
 <?php $wp_query = clone $temp_query; ?>

然而,第二个例子不能在WordPress 2.1中运行。

多个 Loops 例 3 – 插件[ ]

经发现许多的插件并不能与多个loops合作。在这些情况下,经发现,许多的处理文章的关键词和标签的插件,只对文章第一个包含全部文章的子集的一个loop起作用。如果你发现这是一个问题,你可能想要尝试一下添加了update_post_caches($posts)功能的基本的loop在以下的执行情况。这个功能重新设置了文章高速缓存,而且至今没有被证明。只有第一个loop重新得到一个文章的子集,才会执行第二个loop。

仅仅修改:

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

变成:

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

运转的多个Loops[ ]

了解怎样使用多个loops的最好的方法,就是显示一个多个loops的使用的一个例子。也许多个loops最常用的就是在一个网页上显示两个(或者更多的)文章列表。当一个WEB站点管理员不仅想要显示最近所写的文章,还想要显示属于某一类别的文章的时候,便要用到这个。

将所有的形式和CSS问题放到一边,假如我们只想要文章的两个列表。一个会列出最近的文章(最近添加的10个标准的文章),另一个列表只会包含一个来自类别‘特色的’文章。属于‘特色的’类别里的文章应该首先显示,紧跟着,第二个文章的列表(标准的)。 所获得的是没有一篇文章同时出现在两个类别中。

第一步. 从‘有特色的’类中,只拿出一篇文章。

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

在英文中,上述的代码会读作:

$my_query设置为与类别是特色的里的所有文章的查询结果相同,顺便说一下,只给我一篇文章。也将变数$do_not_duplicate设置为与返回的单个文章的ID数字相等。记起,Do stuff行代表了所有的与重新得到的文章相关的格式的选项。

注意我们需要下一步中的$do_not_duplicate值,以确保相同的文章没有在两个类别中重复出现。

第二步. 第二个loop,得到了X篇最新的文章(除了一篇)。

下面的代码得到了最新的X篇文章(如WordPress参数选择中所定义的那样),保存了已经在第一个loop中显示的那一篇,并且根据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; ?>

在英文中,以上的代码会读作:

得到所有的文章,在文章等于$do_not_duplicate的位置,什么措施也别采取,否则的话,根据Do stuff显示所有的其它的文章。而且,更新高速缓存,这样标签和关键词会运行的很好。记住,$do_not_duplicate参数包含了已经显示的文章的ID.

最后的结果

以下是最后的代码是怎样显示的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; ?>

最后的结果是一篇文章有两个列表。第一个列表只包含了一篇文章—来自'特色'的最近的文章。第二个列表会包含X篇最近的文章(如WordPress参数中所定义的)except已经在第一个列表中显示的文章。因此,一旦特色文章被另一个代替了,前一个特色文章就会在下面的标准的文章列表中显示(这取决于你选择显示多少篇文章和文章的频率)。这种方法(或者相似的)由许多人使用,与模板层级的知识想结合,为home.php 和 index.php创建不同的外观。在这一个页面的底部看看相关的资源。

原始资料:

这篇关于多个loops的文章是Ryan BorenAlex King's 的结合体。

http://comox.textdrive.com/pipermail/hackers/2005-January/003578.html 讨论]关于Hackers Mailing List上的loops以及在MaxPower上所写的指南。


更多的 Loop 资源[ ]

学习更多的WordPress Loop, 以及只在Loop内部运行的不同的模板,这儿有一些资源。

资源[ ]


This article is [[WordPress::Category:Copyedits|marked]] as in need of editing. You can help Codex by editing it.