WordPress: 运转的The Loop:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
(新页面: ==Introduction== "The Loop" is a term that refers to the main process of WordPress. You use The Loop in your template files to show posts t...)
 
无编辑摘要
第1行: 第1行:
==Introduction==
==Introduction==
==介绍==
[[WordPress:The Loop|"The Loop"]] is a term that refers to the main process of WordPress.  You use The Loop in your [[WordPress:Templates|template files]] to show posts to visitors.  You could make templates without The Loop, but you'd only be able to display data from one post.
[[WordPress:The Loop|"The Loop"]] is a term that refers to the main process of WordPress.  You use The Loop in your [[WordPress:Templates|template files]] to show posts to visitors.  You could make templates without The Loop, but you'd only be able to display data from one post.
[[WordPress:The Loop|"The Loop"]]是一个术语,指的是WordPress的主要程序。你在[[WordPress:Templates|模板文件]]中使用Loop,来想你的访客显示文章。你可以不用Loop,来制作模板,但是你只能从一篇文章中显示数据。


The first thing WordPress does is check that all the files it needs are present.  Next, it collects the default settings, as defined by the [[WordPress:Registered_User_Features|blog administrator]], from the database.  This includes things like the number of posts to display per page, whether commenting is enabled, and the like.  Once these defaults are established, WordPress checks to see what the user asked for.  This information is used to determine which posts to fetch from the database.
The first thing WordPress does is check that all the files it needs are present.  Next, it collects the default settings, as defined by the [[WordPress:Registered_User_Features|blog administrator]], from the database.  This includes things like the number of posts to display per page, whether commenting is enabled, and the like.  Once these defaults are established, WordPress checks to see what the user asked for.  This information is used to determine which posts to fetch from the database.
WordPress首先要做的就是检查一下,它所需要的文件都已经呈现了。然后,WordPress收集一下,数据库中的[[WordPress:Registered_User_Features|博客 管理员]]所定义的默认设置。这些默认设置包括,每个网页应该显示多少篇文章,是否允许评论,等等。一旦这些默认设置建立了,WordPress便会查看用户需要什么。用户的需求信息会用来决定从数据库中提取那篇文章。


If the user didn't ask for a specific post, category, page, or date, WordPress uses the previously collected default values to determine which posts to prepare for the user.  For example, if the blog administrator has selected to display 5 posts per page in [[WordPress:Administration_Panels|Administration]] > [[WordPress:Administration_Panels#Reading|Settings]] > [[WordPress:Settings_Reading_SubPanel|Reading]], then WordPress will fetch the five most recent posts from the database.  If the user did ask for a specific post, category, page, or date, then WordPress will use that information to specify which ''post''(s) to fetch from the database.
If the user didn't ask for a specific post, category, page, or date, WordPress uses the previously collected default values to determine which posts to prepare for the user.  For example, if the blog administrator has selected to display 5 posts per page in [[WordPress:Administration_Panels|Administration]] > [[WordPress:Administration_Panels#Reading|Settings]] > [[WordPress:Settings_Reading_SubPanel|Reading]], then WordPress will fetch the five most recent posts from the database.  If the user did ask for a specific post, category, page, or date, then WordPress will use that information to specify which ''post''(s) to fetch from the database.
如果用户没有要求要一篇特别的文章,类别,网页,或者日期,WordPress使用先前收集的默认值来决定为用户呈现那篇文章。例如,如果博客管理员选择在[[WordPress:Administration_Panels|管理]] > [[WordPress:Administration_Panels#Reading|设置]] > [[WordPress:Settings_Reading_SubPanel|阅读]]中的每个网页中显示五篇文章,那么WordPress就会在数据库中提取五篇最新的文章。如果用户的确要求要一篇特别的文章,类别,网页,或者日期,那么WordPress会使用这个信息来决定从数据库中提取哪一篇(哪些)''文章''。


Once all this is done, WordPress connects to the database, retrieves the specified information, and stores the results in a variable.  It is The Loop that accesses this variable, and uses the values for display in your templates.
Once all this is done, WordPress connects to the database, retrieves the specified information, and stores the results in a variable.  It is The Loop that accesses this variable, and uses the values for display in your templates.
一旦上述的任务完成了,WordPress就会连接到数据库,重新得到特别的信息,并且用一个变数来储存结果。这是Loop能够使用这个变数,并且使用这个值来显示在你的模板上。


By default, if the visitor did not select a specific post, page, category, or date, WordPress uses <tt>index.php</tt> to display everything.  For the first part of this discussion of The Loop we'll focus only on <tt>index.php</tt>, and the default display of your blog.  Later on, once you understand how things work, we'll investigate tricks with The Loop in other template files.
By default, if the visitor did not select a specific post, page, category, or date, WordPress uses <tt>index.php</tt> to display everything.  For the first part of this discussion of The Loop we'll focus only on <tt>index.php</tt>, and the default display of your blog.  Later on, once you understand how things work, we'll investigate tricks with The Loop in other template files.
默认情况下,如果访客没有选择一篇特别的文章,网页,类别,或者日期,WordPress使用<tt>index.php</tt>来显示所有的内容。对于探讨Loop的第一部分,我们只突出<tt>index.php</tt>,和你的博客的默认显示。然后,你一旦了解了这些内容是怎样运行的,我们就会研究Loop在其它的模板文件中的一些窍门。


==The World's Simplest Index Page==
==The World's Simplest Index Page==
==世界上最简单的搜索页==
The following is a fully functional index which will display the contents (and just the contents) of each post, according to the conditions used to prepare The Loop.  The only purpose for showing you this is to demonstrate how little is actually necessary for the functioning of The Loop.  The bulk of the stuff in your <tt>index.php</tt> is CSS, HTML, and PHP declarations to make The Loop look pretty.
The following is a fully functional index which will display the contents (and just the contents) of each post, according to the conditions used to prepare The Loop.  The only purpose for showing you this is to demonstrate how little is actually necessary for the functioning of The Loop.  The bulk of the stuff in your <tt>index.php</tt> is CSS, HTML, and PHP declarations to make The Loop look pretty.
下面是一个完全性的功能性的索引,这个索引根据用来准备Loop的条件,显示了每篇文章的内容(仅仅是内容)。向你显示这个索引的唯一目的就是,要证明Loop功能运转不怎么必要。你的<tt>index.php</tt>中内容的大部分是,CSS, HTML,和PHP声明来使得Loop看起来更漂亮。
<pre>
&lt;?php
get_header();
if (have_posts()) :
  while (have_posts()) :
      the_post();
      the_content();
  endwhile;
endif;
get_sidebar();
get_footer();
?&gt;
</pre>
<pre>
<pre>
&lt;?php
&lt;?php
第25行: 第62行:
?&gt;
?&gt;
</pre>
</pre>


Now, let's look at the bulk of the stuff that makes The Loop look pretty.
Now, let's look at the bulk of the stuff that makes The Loop look pretty.
现在,让我们来看看这些使得Loop看起来更好看的部分。


==The Default Loop==
==The Default Loop==
==默认的Loop==
The following is a step-by-step look at the default usage of the Loop that comes with the ''default'' and ''classic'' theme in the standard installation of WordPress v1.5.
The following is a step-by-step look at the default usage of the Loop that comes with the ''default'' and ''classic'' theme in the standard installation of WordPress v1.5.
下面内容是关于按部就班地查看Loop的使用,这个Loop伴随在WordPress1.5版本的标准安装的''默认'' 和''经典''主题。


===Begin The Loop===
===Begin The Loop===
===开始 Loop===
Found at the top of the default <tt>index.php</tt> template file is the starting code for  [[WordPress:The Loop]].  
Found at the top of the default <tt>index.php</tt> template file is the starting code for  [[WordPress:The Loop]].  
在默认<tt>index.php</tt>模板文件的顶端是[[WordPress:The Loop|The Loop]]的开始代码。
<pre>&lt;?php if (have_posts()) : ?&gt;<br />
<pre>&lt;?php if (have_posts()) : ?&gt;<br />
<?php while (have_posts()) : the_post(); ?></pre>
<?php while (have_posts()) : the_post(); ?></pre>
<pre>&lt;?php if (have_posts()) : ?&gt;<br />
<?php while (have_posts()) : the_post(); ?></pre>


#First it checks whether any posts were collected with the <tt>have_posts()</tt> function.
#First it checks whether any posts were collected with the <tt>have_posts()</tt> function.
#首先它检查了文章是不是用这个<tt>have_posts()</tt>函数收集了。
#If there are any posts, a PHP <tt>[http://www.php.net/while while]</tt> loop is started.  A <tt>while</tt> loop will continue to execute as long as the condition in the parenthesis is logically true.  So as long as the function <tt>have_posts()</tt> returns a true value, The Loop will keep going.
#If there are any posts, a PHP <tt>[http://www.php.net/while while]</tt> loop is started.  A <tt>while</tt> loop will continue to execute as long as the condition in the parenthesis is logically true.  So as long as the function <tt>have_posts()</tt> returns a true value, The Loop will keep going.
#如果有文章,一个PHP<tt>[http://www.php.net/while while]</tt> loop就会开始运转。只要括号中的条件在逻辑上是真的,那么一个<tt>while</tt> loop就会继续执行。只要函数<tt>have_posts()</tt>返回了一个真值,Loop就会继续运行。
#The function <tt>have_posts()</tt> simply checks the next item in the collection of posts: if there's another item, return true; if there is no next item, return false.
#The function <tt>have_posts()</tt> simply checks the next item in the collection of posts: if there's another item, return true; if there is no next item, return false.
#函数<tt>have_posts()</tt>在文章收集的时候,只会检查下一篇文章:如果有下一篇文章,就会返回真;如果没有下一篇文章,就会返回假。


===Generating the Post===
===Generating the Post===
===产生文章===
The function <tt>the_post()</tt> takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop.  Without <tt>the_post()</tt>, many of the [[WordPress:Template Tags]] used in your theme would not work.
The function <tt>the_post()</tt> takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop.  Without <tt>the_post()</tt>, many of the [[WordPress:Template Tags]] used in your theme would not work.


Once the post data is made available, the template can start showing post data to the visitor.  
Once the post data is made available, the template can start showing post data to the visitor.  
函数<tt>the_post()</tt>在收集文章的时候,拿着当前的文章,并且使得当前的文章在Loop的反复中能够使用。没有了<tt>the_post()</tt>,你的主题中使用的许多的[[WordPress:Template Tags|模板标签]]都不能运行。


====Title, Date and Author====
====Title, Date and Author====
====标题,日期和作者====
The following [[WordPress:Template Tags|template tags]] get the current post's title, as well as the time it was posted and who posted it.
The following [[WordPress:Template Tags|template tags]] get the current post's title, as well as the time it was posted and who posted it.
下面的[[WordPress:Template Tags|模板标签]]得到了当前文章的标题,文章发表的时间,以及谁发表了这篇文章。
<pre>
&lt;h2 id="post-&lt;?php the_ID(); ?&gt;"&gt;
&lt;a href="&lt;?php the_permalink() ?>" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"&gt;
&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;small&gt;&lt;?php the_time('F jS, Y') ?&gt; &lt;!-- by &lt;?php the_author() ?&gt; --&gt;&lt;/small&gt;
</pre>
<pre>
<pre>
&lt;h2 id="post-&lt;?php the_ID(); ?&gt;"&gt;
&lt;h2 id="post-&lt;?php the_ID(); ?&gt;"&gt;
第55行: 第143行:


====Post Content====
====Post Content====
====文章内容====
The <tt>[[WordPress:Template_Tags/the_content|the_content()]]</tt> template tag displays the content of the post. This is the meat and potatoes of each pass through The Loop:
The <tt>[[WordPress:Template_Tags/the_content|the_content()]]</tt> template tag displays the content of the post. This is the meat and potatoes of each pass through The Loop:
<tt>[[WordPress:Template_Tags/the_content|the_内容()]]</tt>模板标签显示了文章的内容。这是通过Loop的每篇文章的最重要的部分。
<pre>
&lt;div class="entry"&gt;
&lt;?php the_content('Read the rest of this entry &amp;raquo;'); ?&gt;
&lt;/div&gt;
</pre>
<pre>
<pre>
&lt;div class="entry"&gt;
&lt;div class="entry"&gt;
第61行: 第165行:
&lt;/div&gt;
&lt;/div&gt;
</pre>
</pre>
If you include the [[WordPress:Write_Post_SubPanel#Quicktags|Quicktag]] button called '''more''', and shown as <tt>[[WordPress:Customizing_the_Read_More|&lt;!--more--&gt;]]</tt>, in the body of your post, only the portion <em>above</em> that line will be displayed to viewers.  So, if you only want your front page to show the first sentence or two of every post, simply insert <tt>&lt;!--more--&gt;</tt> after the first line into every post you make.
If you include the [[WordPress:Write_Post_SubPanel#Quicktags|Quicktag]] button called '''more''', and shown as <tt>[[WordPress:Customizing_the_Read_More|&lt;!--more--&gt;]]</tt>, in the body of your post, only the portion <em>above</em> that line will be displayed to viewers.  So, if you only want your front page to show the first sentence or two of every post, simply insert <tt>&lt;!--more--&gt;</tt> after the first line into every post you make.
如果你包含了称作'''更多的'''[[WordPress:Write_Post_SubPanel#Quicktags|Quicktag]]按钮,而且这个按钮在你的文章主体显示为<tt>[[WordPress:Customizing_the_Read_More|&lt;!--more--&gt;]]</tt>,只有那一行<em>上面</em>的部分才会显示给访客。因此,如果你只想首页显示每篇文章的第一句话,或者前两句话,只要在你所写的文章中的第一行后插入<tt>&lt;!--more--&gt;</tt>,就可以了。


When viewing a single post, the <tt>&lt;!--more--&gt;</tt> delimiter is skipped.  So putting the <tt>&lt;!--more--&gt;</tt> delimiter into all your posts forces readers to click through to each individual post if they want to read the whole thing.
When viewing a single post, the <tt>&lt;!--more--&gt;</tt> delimiter is skipped.  So putting the <tt>&lt;!--more--&gt;</tt> delimiter into all your posts forces readers to click through to each individual post if they want to read the whole thing.
当你阅读一篇单一的文章的时候,<tt>&lt;!--more--&gt;</tt>分隔符就被跳过了。因此在你所有的文章中放入<tt>&lt;!--more--&gt;</tt>分隔符,如果读者想阅读所有的内容的话,这个分隔符会迫使读者来点击每篇单个的文章。


====Additional Details====
====Additional Details====
====额外的详细信息====
Beneath each post's content in the <tt>index.php</tt> template file is a place to display more information about the post, such as the categories, date, and comment information.  Known as the [[WordPress:Post_Meta_Data_Section|post meta data section]], if you're a logged in user of sufficient privilege (or the post's author), you will also see an "Edit This" link, thanks to the <tt>[[WordPress:Template_Tags/edit_post_link|edit_post_link()]]</tt> template tag function.
Beneath each post's content in the <tt>index.php</tt> template file is a place to display more information about the post, such as the categories, date, and comment information.  Known as the [[WordPress:Post_Meta_Data_Section|post meta data section]], if you're a logged in user of sufficient privilege (or the post's author), you will also see an "Edit This" link, thanks to the <tt>[[WordPress:Template_Tags/edit_post_link|edit_post_link()]]</tt> template tag function.
在<tt>index.php</tt>模板文件中的每篇文章的内容下面,有一个位置可以显示文章的更多的信息,例如类别,数据,评论信息。称作[[WordPress:Post_Meta_Data_Section|文章 meta 数据 部分]],如果你是一个注册过的用户,拥有足够的特权(或者文章的作者),你就会看见一个"编辑这个"链接,感谢<tt>[[WordPress:Template_Tags/edit_post_link|edit_post_link()]]</tt> 模板标签函数。
<pre>
<pre>
&lt;p class="postmetadata"&gt;
&lt;p class="postmetadata"&gt;
第74行: 第190行:
&lt;?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?&gt;&lt;/p&gt;
&lt;?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?&gt;&lt;/p&gt;
</pre>
</pre>
<pre>
&lt;p class="postmetadata"&gt;
Posted in &lt;?php the_category(', ') ?&gt;
&lt;strong&gt;|&lt;/strong&gt;
&lt;?php edit_post_link('Edit','','<strong>|</strong>'); ?&gt; 
&lt;?php comments_popup_link('No Comments &#187;', '1 Comment &#187;', '% Comments &#187;'); ?&gt;&lt;/p&gt;
</pre>


If commenting is enabled, or if the post has comments, the <tt>[[WordPress:Template_Tags/comments_popup_link|comments_popup_link()]]</tt> template tag will display a link to the comments.  If you're using the [[WordPress:Template_Tags/comments_popup_script|comments popup window]], this link will open the comments window; otherwise it will jump right to this post's comments.
If commenting is enabled, or if the post has comments, the <tt>[[WordPress:Template_Tags/comments_popup_link|comments_popup_link()]]</tt> template tag will display a link to the comments.  If you're using the [[WordPress:Template_Tags/comments_popup_script|comments popup window]], this link will open the comments window; otherwise it will jump right to this post's comments.
如果写评论得到了激活,或者如果文章拥有评论,<tt>[[WordPress:Template_Tags/comments_popup_link|comments_popup_link()]]</tt>模板标签就会显示一个连接到评论的链接。如果你正在使用[[WordPress:Template_Tags/comments_popup_script|评论弹出窗口]],这个链接会打开评论窗口,否则的话,它就会跳到这篇关于这篇文章的评论。


If the visitor is viewing an index of posts (''i.e.:'' more than one post in The Loop), the <tt>comments_popup_link()</tt> link will take the reader to this post's individual page.
If the visitor is viewing an index of posts (''i.e.:'' more than one post in The Loop), the <tt>comments_popup_link()</tt> link will take the reader to this post's individual page.
如果访客正在访问文章的索引(''例如:'' Loop中有几篇文章), <tt>comments_popup_link()</tt>链接会将读者带到这篇文章的单个的网页。


====Trackback Autodiscovery====
====Trackback Autodiscovery====
====Trackback 自动查找====
The <tt>[[WordPress:Template_Tags/trackback_rdf|trackback_rdf]]</tt> template tag's function is to output machine-readable code used for [[WordPress:Glossary#Trackback|trackback]] auto-discovery.
The <tt>[[WordPress:Template_Tags/trackback_rdf|trackback_rdf]]</tt> template tag's function is to output machine-readable code used for [[WordPress:Glossary#Trackback|trackback]] auto-discovery.
<tt>[[WordPress:Template_Tags/trackback_rdf|trackback_rdf]]</tt>模板标签的功能是要产出用于[[WordPress:Glossary#Trackback|trackback]]自动查找的,机器能够识别的代码。
<pre>
&lt;!--
&lt;?php trackback_rdf(); ?&gt;
--&gt;
</pre>


<pre>
<pre>
第87行: 第234行:
--&gt;
--&gt;
</pre>
</pre>


'''Note:''' The <tt>trackback_rdf()</tt> tag is supposed to be used with [[WordPress:Commenting_Code|comments]] around it.  It is not "turned off".
'''Note:''' The <tt>trackback_rdf()</tt> tag is supposed to be used with [[WordPress:Commenting_Code|comments]] around it.  It is not "turned off".
'''注:''' <tt>trackback_rdf()</tt>标签使用时,周围应该有[[WordPress:Commenting_Code|评论]]。


===Ending The Loop===
===Ending The Loop===
===编辑Loop===
The following ends The Loop.  After this, the various post-related template tags will not work as expected (or if they do, they will use the last post from The Loop). This means, that if you need to use a template tag that works '''within The Loop''', you need to put it in before this point.
The following ends The Loop.  After this, the various post-related template tags will not work as expected (or if they do, they will use the last post from The Loop). This means, that if you need to use a template tag that works '''within The Loop''', you need to put it in before this point.
以下的函数结束了Loop。这个函数执行之后,不同的与文章相关的模板标签,就不会像期望地那样运行(或者如果这些模板运行的话,它们会使用Loop中的最后一篇文章)。这意味着,如果你要使用一个模板标签,这个标签在''' Loop内部'''运行,你就要将这个标签放在这一点之前。
<pre>
<pre>
&lt;?php endwhile; ?&gt;
&lt;?php endwhile; ?&gt;
</pre>
</pre>
<pre>
&lt;?php endwhile; ?&gt;
</pre>


This section, immediately after the end of The Loop, displays navigation controls to move forward and backward by each web page.
This section, immediately after the end of The Loop, displays navigation controls to move forward and backward by each web page.
这个部分,就在结束Loop的后面,显示了通过每个网页而前进或者后退的导航控制。
<pre>
&lt;div class="navigation"&gt;
&lt;div class="alignleft"&gt;&lt;?php posts_nav_link('','','&amp;laquo; Previous Entries') ?&gt;&lt;/div&gt;
&lt;div class="alignright"&gt;&lt;?php posts_nav_link('','Next Entries &amp;raquo;','') ?&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
<pre>
<pre>
&lt;div class="navigation"&gt;
&lt;div class="navigation"&gt;
第105行: 第279行:


If the blog is set to display 10 posts per page, and the conditions used by The Loop collect 25 posts, there will be three pages to navigate: two pages of 10 posts each, and one page of 5 posts.  The navigation links will allow the visitor to move forward and backward through the collection of posts.
If the blog is set to display 10 posts per page, and the conditions used by The Loop collect 25 posts, there will be three pages to navigate: two pages of 10 posts each, and one page of 5 posts.  The navigation links will allow the visitor to move forward and backward through the collection of posts.
如果博客被设置为每个网页显示十篇文章,而且Loop使用的条件收集了25篇文章,那么会有三个网页需要导航:其中两个网页,每个网页有十篇文章,还有一个网页有五篇文章。导航链接能够使访客在收集的文章中,向前或者向后翻看。


The navigation controls are included <em>outside</em> The Loop, but <em>inside</em> the <tt>if</tt> condition, so that they only show up if there are any posts.  The navigation functions themselves also check whether or not there is anything to which they will link, based on the current Loop, and only display links if there's something to link.
The navigation controls are included <em>outside</em> The Loop, but <em>inside</em> the <tt>if</tt> condition, so that they only show up if there are any posts.  The navigation functions themselves also check whether or not there is anything to which they will link, based on the current Loop, and only display links if there's something to link.
导航控制<em>不</em>包含在Loop中,但是包含<em>在</em><tt>假设</tt>条件中,因此只有有文章的时候,导航控制才会出现。导航函数本身也会以当前的Loop为基础,查看有没有它们可以链接的内容,而且只有在有一些可以链接的内容的情况下,导航函数才会显示链接。
<pre>
&lt;?php else : ?&gt;
&lt;h2 class="center"&gt;Not Found&lt;/h2&gt;
&lt;p class="center"&gt;
&lt;?php _e("Sorry, but you are looking for something that isn't here."); ?&gt;&lt;/p&gt;
</pre>


<pre>
<pre>
第114行: 第302行:
&lt;?php _e("Sorry, but you are looking for something that isn't here."); ?&gt;&lt;/p&gt;
&lt;?php _e("Sorry, but you are looking for something that isn't here."); ?&gt;&lt;/p&gt;
</pre>
</pre>
The <tt>else :</tt> clause determines what to do if <tt>have_posts()</tt> (from way up at the top) is false.  That is to say, the stuff after the '''else''' will only be executed/displayed if The Loop had zero posts. No posts show up if, for example, the visitor requested a specific day for which no posts were made or a search was performed that produced no results.
The <tt>else :</tt> clause determines what to do if <tt>have_posts()</tt> (from way up at the top) is false.  That is to say, the stuff after the '''else''' will only be executed/displayed if The Loop had zero posts. No posts show up if, for example, the visitor requested a specific day for which no posts were made or a search was performed that produced no results.
<tt>其它的 :</tt>字句决定了如果<tt>有_文章()</tt>(在顶端上方)是假的,应该执行什么。这就是说,只有Loop中没有文章的时候,'''其它的'''后面的内容才会得到执行/显示。例如,如果访客需要某天的文章,但是这一天没有文章,或者经过了搜索,但是没有产生结果,没有显示文章。
<pre>
  &lt;?php endif; ?&gt;
</pre>


<pre>
<pre>
   &lt;?php endif; ?&gt;
   &lt;?php endif; ?&gt;
</pre>
</pre>
This ends the conditional test of "if there are posts do this, else if there are no posts, do that".  Once the conditional test is finished, the default index.php template next includes the sidebar, and finally the footer.
This ends the conditional test of "if there are posts do this, else if there are no posts, do that".  Once the conditional test is finished, the default index.php template next includes the sidebar, and finally the footer.
这个结束了条件测试"如果有文章,这样执行,如果没有文章,那样执行"。一旦条件测试结束了,默认的index.php模板然后包含工具条,最后包含页脚。


==The Loop In Other Templates==
==The Loop In Other Templates==
==其它的模板中的Loop ==
WordPress can use different template files for displaying your blog in different ways.  In the default WordPress theme, there are [[WordPress:Templates|template files]] for the index view, category view, and archive view, as well as a template for viewing individual posts.  Each of these uses [[WordPress:The Loop]], but does so with slightly different formatting, as well as different uses of the [[WordPress:Template_Tags|template tags]].
WordPress can use different template files for displaying your blog in different ways.  In the default WordPress theme, there are [[WordPress:Templates|template files]] for the index view, category view, and archive view, as well as a template for viewing individual posts.  Each of these uses [[WordPress:The Loop]], but does so with slightly different formatting, as well as different uses of the [[WordPress:Template_Tags|template tags]].
WordPress可以使用不同的模板文件以不同的方式来显示你的博客。在WordPress默认主题中,有关于索引浏览,类别浏览和归档浏览的[[WordPress:Templates|模板文件]],也有一个浏览个人文章的模板。这些模板和模板文件都使用[[WordPress:The Loop]],但是使用的格式有所不同,而且对[[WordPress:Template_Tags|模板标签]]的使用也有所不同。


For any view which does not have a separate template file, WordPress will use <tt>index.php</tt> by default.  If a visitor requests a single post, WordPress will first look for a file named <tt>single.php</tt>.  If that file exists, it will be used to present the post to the visitor.  If that file does not exist, WordPress will use <tt>index.php</tt> to present the post to the visitor. This is called the [[WordPress:Template Hierarchy]].
For any view which does not have a separate template file, WordPress will use <tt>index.php</tt> by default.  If a visitor requests a single post, WordPress will first look for a file named <tt>single.php</tt>.  If that file exists, it will be used to present the post to the visitor.  If that file does not exist, WordPress will use <tt>index.php</tt> to present the post to the visitor. This is called the [[WordPress:Template Hierarchy]].
对于没有一个单独的模板文件的浏览,WordPress会默认使用<tt>index.php</tt>。如果一位访客需要一篇单一的文章,WordPress会首先查看一个名称为<tt>single.php</tt>的文件。如果这个文件存在的话,这个文件就会被用来向访客呈现文章。如果这个文件不存在,WordPress会使用<tt>index.php</tt>来向访客呈现文章。这叫做[[WordPress:Template Hierarchy|模板层级]]。


If you are making your own [[WordPress:Using Themes|Theme]], it's often helpful to look at the [[WordPress:Templates|template files]] from the default Theme as a point of reference.  It's also helpful to use your theme's <tt>index.php</tt> as a template for your other template files.  Doing so may give you a known and working page from which to begin making changes as you create more template files.
If you are making your own [[WordPress:Using Themes|Theme]], it's often helpful to look at the [[WordPress:Templates|template files]] from the default Theme as a point of reference.  It's also helpful to use your theme's <tt>index.php</tt> as a template for your other template files.  Doing so may give you a known and working page from which to begin making changes as you create more template files.
如果你正制作你自己的[[WordPress:Using Themes|主题]],看看默认主题中作为参考的[[WordPress:Templates|模板文件]]是很有用的。为你的其它模板文件使用你的主题的<tt>index.php</tt>作为一个模板,也很有用。执行了这些之后,你可能得到了一个已知的,并且正在运行的网页,当你创建更多的模板文件的时候,这个网页就会发生变化。


===Different Archive Format===
===Different Archive Format===
===不同的归档样式===
An <em>archive</em> is a collection of historical posts.  In the default usage, the posts displayed on your main index are recent [http://mydatapages.com/chronological.html chronological] postings.  When a visitor clicks on one of your archive links, or if they manually request a specific date (<nowiki>http://www.example.com/blog/index.php?m=200504</nowiki> or <nowiki>http://www.example.com/blog/2005/04</nowiki> to select all posts from April, 2005), WordPress will display an <em>archive</em> view.  By default, the archive will use <tt>index.php</tt>, and thus look the same as your front page, just displaying the posts from April 2005.
An <em>archive</em> is a collection of historical posts.  In the default usage, the posts displayed on your main index are recent [http://mydatapages.com/chronological.html chronological] postings.  When a visitor clicks on one of your archive links, or if they manually request a specific date (<nowiki>http://www.example.com/blog/index.php?m=200504</nowiki> or <nowiki>http://www.example.com/blog/2005/04</nowiki> to select all posts from April, 2005), WordPress will display an <em>archive</em> view.  By default, the archive will use <tt>index.php</tt>, and thus look the same as your front page, just displaying the posts from April 2005.
一个<em>归档</em>是对过去的文章的收集。在默认使用中,显示在你的主索引上的文章是最近[http://mydatapages.com/chronological.html 按时间顺序的]发表的文章。当访客点击你其中的一个归档链接的时候,或者如果访客手工操作要求一个特备的日期(<nowiki>http://www.example.com/blog/index.php?m=200504</nowiki> or <nowiki>http://www.example.com/blog/2005/04</nowiki> 选择从2005年四月开始的所有的文章),WordPress会显示一个<em>档案</em> 浏览。默认情况下,档案会使用<tt>index.php</tt>,因此档案看起来和你的首页是相同的,只显示从2005年四月开始的文章。


When WordPress prepares an [[WordPress:Creating_an_Archive_Index|archive view]] for a visitor, it specifically looks for a file named <tt>archive.php</tt> in your current theme's directory.  If you'd like to visually disambiguate archives from your front page, simply copy <tt>index.php</tt> to <tt>archive.php</tt>, and edit <tt>archive.php</tt> as necessary!
When WordPress prepares an [[WordPress:Creating_an_Archive_Index|archive view]] for a visitor, it specifically looks for a file named <tt>archive.php</tt> in your current theme's directory.  If you'd like to visually disambiguate archives from your front page, simply copy <tt>index.php</tt> to <tt>archive.php</tt>, and edit <tt>archive.php</tt> as necessary!
当WordPress为一名访客准备一个[[WordPress:Creating_an_Archive_Index|档案浏览]]的时候,WordPress特别要查找你当前主题目录中一个名为<tt>archive.php</tt>的文件。如果你想真实地消除首页中的档案的歧义,你只要复制<tt>index.php</tt>到<tt>archive.php</tt>,并且需要的话,编辑<tt>archive.php</tt>!


For example, if you want to show only post titles, and no post content, for your list of archives, you could use something like this:
For example, if you want to show only post titles, and no post content, for your list of archives, you could use something like this:
例如,如果你只想在你的文章档案列表上显示文章标题,而不是文章内容,你可能要使用以下的内容:
<pre>
<pre>
&lt;?php get_header(); ?&gt;
&lt;?php get_header(); ?&gt;
第162行: 第389行:
&lt;?php get_footer(); ?&gt;
&lt;?php get_footer(); ?&gt;
</pre>
</pre>
<pre>
&lt;?php get_header(); ?&gt;
&lt;div id="content" class="narrowcolumn"&gt;
  &lt;?php if (have_posts()) : ?&gt;
  &lt;?php while (have_posts()) : the_post(); ?&gt;
    &lt;div class="post"&gt;
    &lt;h2 id="post-&lt;?php the_ID(); ?&gt;"&gt;
&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h2&gt;
    &lt;small&gt;&lt;?php the_time('F jS, Y') ?&gt; &lt;!-- by &lt;?php the_author() ?&gt; --&gt;&lt;/small&gt;
      &lt;/div&gt;
    &lt;?php endwhile; ?&gt;
&lt;div class="navigation"&gt;
&lt;div class="alignleft"&gt;
&lt;?php posts_nav_link('','','&amp;laquo; Previous Entries') ?&gt;
&lt;/div&gt;
&lt;div class="alignright"&gt;
&lt;?php posts_nav_link('','Next Entries &amp;raquo;','') ?&gt;
&lt;/div&gt;
  &lt;/div&gt;
&lt;?php else : ?&gt;
  &lt;h2 class="center"&gt;Not Found&lt;/h2&gt;
&lt;p class="center"&gt;&lt;?php _e("Sorry, but you are looking for something that isn't here."); ?&gt;&lt;/p&gt;
  &lt;?php endif; ?&gt;
&lt;/div&gt;
&lt;?php get_sidebar(); ?&gt;
&lt;?php get_footer(); ?&gt;
</pre>


===Different Category Format===
===Different Category Format===
===不同的类别样式===
Like the archive views, WordPress looks for a separate template file for [[WordPress:Category_Templates|category views]].  If a visitor clicks on a link for a category in your blog, they will be taken to the category view.  WordPress will prepare The Loop with posts from that category only, limiting the number of posts per the blog's default settings.
Like the archive views, WordPress looks for a separate template file for [[WordPress:Category_Templates|category views]].  If a visitor clicks on a link for a category in your blog, they will be taken to the category view.  WordPress will prepare The Loop with posts from that category only, limiting the number of posts per the blog's default settings.
与档案浏览相似,WordPress为[[WordPress:Category_Templates|类别浏览]]查找一个单独的模板文件。如果一个访客点击你的博客上一个类别的一个链接,访客就会来到类别浏览。WordPress会给Loop准备来自那个类别的文章,并且根据博客的默认设置来限制文章的数目。


To make your category view different from your index view, copy <tt>index.php</tt> and rename it <tt>category.php</tt>.  For a category view, it's probably not necessary to list the categories to which a post is assigned, so let's remove that portion.  Instead, let's announce the category at the top of the page:
To make your category view different from your index view, copy <tt>index.php</tt> and rename it <tt>category.php</tt>.  For a category view, it's probably not necessary to list the categories to which a post is assigned, so let's remove that portion.  Instead, let's announce the category at the top of the page:
要使你的类别浏览与你的索引浏览有所区别,复制<tt>index.php</tt>并且将它重新命名为<tt>category.php</tt>。对于一个类别浏览,可能没有必要列出文章所在的类别,因此我们移除这个部分。反之,让我们在网页的顶上方公布类别:
<pre>
<pre>
&lt;?php get_header(); ?&gt;
&lt;?php get_header(); ?&gt;
第204行: 第479行:
&lt;?php get_footer(); ?&gt;
&lt;?php get_footer(); ?&gt;
</pre>
</pre>
<pre>
&lt;?php get_header(); ?&gt;
&lt;div id="content" class="narrowcolumn"&gt;
&lt;p&gt;
&lt;strong&gt;
  &lt;?php single_cat_title('Currently browsing '); ?&gt;
  &lt;/strong&gt;&lt;br /&gt;
&lt;?php echo category_description(); ?&gt;
&lt;/p&gt;
&lt;?php if (have_posts()) : ?&gt;
  &lt;?php while (have_posts()) : the_post(); ?&gt;
    &lt;div class="post"&gt;
      &lt;h2 id="post-&lt;?php the_ID(); ?&gt;"&gt;
&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="Permanent Link to &lt;?php the_title(); ?&gt;"&gt;
&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h2&gt;
  &lt;small&gt;
    &lt;?php the_time('F jS, Y') ?&gt;
        &lt;!-- by &lt;?php the_author() ?&gt; --&gt;
  &lt;/small&gt;
&lt;/div&gt;
&lt;?php endwhile; ?&gt;
&lt;div class="navigation"&gt;
  &lt;div class="alignleft"&gt;
    &lt;?php posts_nav_link('','','&amp;laquo; Previous Entries') ?&gt;
  &lt;/div&gt;
  &lt;div class="alignright"&gt;
    &lt;?php posts_nav_link('','Next Entries &amp;raquo;','') ?&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;?php else : ?&gt;
  &lt;h2 class="center"&gt;Not Found&lt;/h2&gt;
&lt;p class="center"&gt;&lt;?php _e("对不起, 但是你找的东西,不在这儿。"); ?&gt;&lt;/p&gt;
&lt;?php endif; ?&gt;
&lt;/div&gt;
&lt;?php get_sidebar(); ?&gt;
&lt;?php get_footer(); ?&gt;
</pre>


===Different Formats for Different Categories===
===Different Formats for Different Categories===
===不同类别的不同样式===
As explained in the [[WordPress:Template Hierarchy]], it is possible to [[WordPress:Category_Templates|create separate template files for each category]].  Simply name the file <tt>category-<b><u>X</u></b>.php</tt>, where <b><u>X</u></b> is the numerical ID of the category.  Consider carefully whether you need a whole new template for a specific category.
As explained in the [[WordPress:Template Hierarchy]], it is possible to [[WordPress:Category_Templates|create separate template files for each category]].  Simply name the file <tt>category-<b><u>X</u></b>.php</tt>, where <b><u>X</u></b> is the numerical ID of the category.  Consider carefully whether you need a whole new template for a specific category.
就如[[WordPress:Template Hierarchy|模板层级]]里所解释的那样, [[WordPress:Category_Templates|为每个类别创建单独的模板文件]]时可能的。只要命名文件<tt>category-<b><u>X</u></b>.php</tt>,其中<b><u>X</u></b>是类别的数字ID。认真考虑你是否需要为一个特别的类别准备一个全新的模板。


Let's look at two categories, "Plants" and "Flowers", with category IDs 3 and 4, respectively.  Next to each post title in the output you want to have picture of either a plant, or a flower, depending on which category is being displayed.  You could:
Let's look at two categories, "Plants" and "Flowers", with category IDs 3 and 4, respectively.  Next to each post title in the output you want to have picture of either a plant, or a flower, depending on which category is being displayed.  You could:
让我们来看看两个类别,"植物"和 "花",各自的类别ID是3和4。在每个文章标题旁边的输出中,取决于你正在显示的类别,你可能想要一颗植物或者一朵花的图片。你可以:
* Use two separate files, <tt>category-3.php</tt> and <tt>category-4.php</tt>, each with a different <tt>img</tt> tag for each post title.
* Use two separate files, <tt>category-3.php</tt> and <tt>category-4.php</tt>, each with a different <tt>img</tt> tag for each post title.
*使用两个单独的文件,<tt>类别-3.php</tt> 和 <tt>类别-4.php</tt>,每个类别中每篇文章的标题都有一个不同的<tt>img</tt>标签。
* Use a conditional test inside your default <tt>category.php</tt> file to check whether the current category is "Plants" or "Flowers" (or neither), and display the appropriate image:
* Use a conditional test inside your default <tt>category.php</tt> file to check whether the current category is "Plants" or "Flowers" (or neither), and display the appropriate image:
*在你的默认的<tt>category.php</tt>文件中使用一个条件测试来看看当前的类别是"植物" 还是 "花朵"(或者都不是),并且显示适当的图像:
<pre>
<pre>
&lt;?php if (is_category('3') ):
&lt;?php if (is_category('3') ):
第217行: 第557行:
&lt;?php } elseif (is_category('4') ):
&lt;?php } elseif (is_category('4') ):
  // we're in the Flowers category, so show a flower ?&gt;
  // we're in the Flowers category, so show a flower ?&gt;
&lt;img src='/images/flower.png' alt='a pretty flower' /&gt;
&lt;?php endif; // end the if, no images for other other categories ?&gt;
</pre>
<pre>
&lt;?php if (is_category('3') ):
// 我们在植物类别,因此显示一颗植物 ?&gt;
&lt;img src='/images/plant.png' alt='a plant' /&gt;
&lt;?php } elseif (is_category('4') ):
// 我们在花朵类别,因此显示一朵花?&gt;
  &lt;img src='/images/flower.png' alt='a pretty flower' /&gt;
  &lt;img src='/images/flower.png' alt='a pretty flower' /&gt;
&lt;?php endif; // end the if, no images for other other categories ?&gt;
&lt;?php endif; // end the if, no images for other other categories ?&gt;
第222行: 第573行:


If you added another category, "Cars", which you wanted to display in a <em>significantly</em> different way, then a separate <tt>category-<b><u>X</u></b>.php</tt> would be more appropriate.
If you added another category, "Cars", which you wanted to display in a <em>significantly</em> different way, then a separate <tt>category-<b><u>X</u></b>.php</tt> would be more appropriate.
如果你添加了另一个类别,"汽车",你想要以<em>非常</em>不同的方式来显示这个类别,那么一个分开的<tt>类别-<b><u>X</u></b>.php</tt>会更加地合适。


=== Different CSS For Different Categories ===
=== Different CSS For Different Categories ===
=== 不同类别的不同的CSS ===


Many users want to create separate CSS files for a specific category.  This, too, can be easily accomplished.  It is important to remember that stylesheets are defined and loaded in the <tt>&lt;head&gt;</tt> section of the HTML document.  WordPress uses the <tt>header.php</tt> file for this.  In the default <tt>header.php</tt>, find this line:
Many users want to create separate CSS files for a specific category.  This, too, can be easily accomplished.  It is important to remember that stylesheets are defined and loaded in the <tt>&lt;head&gt;</tt> section of the HTML document.  WordPress uses the <tt>header.php</tt> file for this.  In the default <tt>header.php</tt>, find this line:
许多用户想要为一个特别的类别创建单独的CSS文件。这一点也很容易实现。记住样式表是在HTML文件的<tt>&lt;head&gt;</tt>中定义和载入的,这一点很重要。WordPress可以为这个使用<tt>header.php</tt>文件。在默认<tt>header.php</tt>,找到这一行:
<pre>
&lt;link rel="stylesheet" href="&lt;?php bloginfo('stylesheet_url'); ?&gt;" type="text/css" media="screen" /&gt;
</pre>
<pre>
<pre>
&lt;link rel="stylesheet" href="&lt;?php bloginfo('stylesheet_url'); ?&gt;" type="text/css" media="screen" /&gt;
&lt;link rel="stylesheet" href="&lt;?php bloginfo('stylesheet_url'); ?&gt;" type="text/css" media="screen" /&gt;
</pre>
</pre>
And change it to something like this:
And change it to something like this:
将这个改为:
<pre>
&lt;?php if ( is_category('5') ) { // Load special CSS for "Cars" category ?&gt;
  <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/category-5.css" type="text/css" media="screen" />;
&lt;?php } else { ?&gt;
  <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
&lt;?php } ?&gt;
</pre>
<pre>
<pre>
&lt;?php if ( is_category('5') ) { // Load special CSS for "Cars" category ?&gt;
&lt;?php if ( is_category('5') ) { // Load special CSS for "Cars" category ?&gt;
第237行: 第613行:
&lt;?php } ?&gt;
&lt;?php } ?&gt;
</pre>
</pre>
<strong>Note:</strong> The Cars template uses the <tt>category-5.css</tt> file to override the default layout.  In this example the CSS file is named after the category template file to which it will be applied, rather than the actual name of the category.  Thus, you know that <tt>category-5.css</tt> goes with <tt>category-5.php</tt>.
<strong>Note:</strong> The Cars template uses the <tt>category-5.css</tt> file to override the default layout.  In this example the CSS file is named after the category template file to which it will be applied, rather than the actual name of the category.  Thus, you know that <tt>category-5.css</tt> goes with <tt>category-5.php</tt>.
<strong>注:</strong>汽车模板使用<tt>类别-5.css</tt>文件来覆盖默认设置。在这个例子中,CSS文件命名在类别模板文件之后,而不是正真的类别名,CSS文件将会用在类别模板文件中。因此,你知道<tt>category-5.css</tt>和<tt>category-5.php</tt>在一起。


===Different Single Post Format===
===Different Single Post Format===
===不同的单个文章格式===
When viewing any single post (or [[WordPress:Glossary#Permalink|permalink]]), WordPress will use <tt>single.php</tt>, if present.
When viewing any single post (or [[WordPress:Glossary#Permalink|permalink]]), WordPress will use <tt>single.php</tt>, if present.
当访问任何单一的文章(或者[[WordPress:Glossary#Permalink|permalink]]),WordPress会使用<tt>single.php</tt>。


This portion, from the WordPress default single.php, provides the [[WordPress:Post_Meta_Data_Section|post meta data information]] about the current post:
This portion, from the WordPress default single.php, provides the [[WordPress:Post_Meta_Data_Section|post meta data information]] about the current post:
来自WordPress默认single.php的这个部分,提供了关于当前文章的[[WordPress:Post_Meta_Data_Section|文章 meta 数据 信息]]:
<pre>
<pre>
<p class="postmetadata alt">
<p class="postmetadata alt">
<small>
<small>
<pre>
<p class="postmetadata alt">
<small>
This entry was posted on &lt;?php the_time('l, F jS, Y') ?&gt; at &lt;?php the_time() ?&gt;  
This entry was posted on &lt;?php the_time('l, F jS, Y') ?&gt; at &lt;?php the_time() ?&gt;  
and is filed under &lt;?php the_category(', ') ?&gt;.
and is filed under &lt;?php the_category(', ') ?&gt;.
You can follow any responses to this entry through  
You can follow any responses to this entry through  
the &lt;?php comments_rss_link('RSS 2.0'); ?&gt; feed.
the &lt;?php comments_rss_link('RSS 2.0'); ?&gt; feed.
这篇文章发表在&lt;?php the_time('l, F jS, Y') ?&gt;在 &lt;?php the_time() ?&gt;而且归档在&lt;?php the_category(', ') ?&gt;。通过&lt;?php comments_rss_link('RSS 2.0'); ?&gt; feed,你可以遵循任何对于这篇文章的响应。
&lt;?php
&lt;?php
if (('open' == $post-&gt;comment_status) && ('open' == $post->ping_status)) {
if (('open' == $post-&gt;comment_status) && ('open' == $post->ping_status)) {
第280行: 第679行:
&lt;/p&gt;
&lt;/p&gt;
</pre>
</pre>
&lt;?php
if (('open' == $post-&gt;comment_status) && ('open' == $post->ping_status)) {
// 评论和 Pings都打开了
?&gt;
  你可以 &lt;a href="#respond"&gt;leave a response&lt;/a&gt;,或者
  &lt;a href="&lt;?php trackback_url(display); ?&gt;"&gt;trackback&lt;/a&gt;
来自你自己的站点。
&lt;?php
} elseif (!('open' == $post-&gt;comment_status) && ('open' == $post->ping_status)) {
// 只有 Pings 是打开的。
?&gt;
  回响当前关闭了,但是你可以从你自己的站点上  &lt;a href="&lt;?php trackback_url(display); ?&gt; "&gt;trackback&lt;/a&gt;
&lt;?php
} elseif (('open' == $post-&gt;comment_status) && !('open' == $post->ping_status)) {
// 评论是打开的,但是Ping没有。
?&gt;
  你可以跳读到最后,并且留下一个响应。Ping当前不被允许。&lt;?php
} elseif (!('open' == $post-&gt;comment_status) && !('open' == $post->ping_status)) {
// 评论和Ping都没有打开
?&gt;
  评论和pings当前都关闭了。&lt;?php
}
edit_post_link('编辑这篇文章。','',''); ?&gt;
&lt;/small&gt;
&lt;/p&gt;
</pre>
This sort of information -- whether comments are open or closed -- is largely inappropriate on an index, archive, or category view; which is why it's only included in the <tt>single.php</tt> template file.
This sort of information -- whether comments are open or closed -- is largely inappropriate on an index, archive, or category view; which is why it's only included in the <tt>single.php</tt> template file.
这种形式的信息—不过评论是打开的还是关闭的—对于一个索引,归档,或者类别浏览是不合适的;这就是这种信息只包含在<tt>single.php</tt>模板文件中的原因。


==Other Loop Tricks==
==Other Loop Tricks==
==其它的 Loop Tricks==
Now that you have a good introduction to the basic uses for the WordPress Loop, let's introduce you to some more Loop effects and tricks.
Now that you have a good introduction to the basic uses for the WordPress Loop, let's introduce you to some more Loop effects and tricks.
既然对于WordPress Loop的基本用户,你有一个好的介绍说明。让我们给你引入更多的Loop effects和tricks。


===Static Front Page===
===Static Front Page===
===静态的首页===


How can you display something special <em>only</em> on the front page of your blog? That's right, only on the front page or home page, and have it not be seen anywhere else on your site. Easy! We call this the ''static front page''.  The front or first page of your site isn't really static.  It's just using the Loop to make it look that way.
How can you display something special <em>only</em> on the front page of your blog? That's right, only on the front page or home page, and have it not be seen anywhere else on your site. Easy! We call this the ''static front page''.  The front or first page of your site isn't really static.  It's just using the Loop to make it look that way.
你怎样显示一些特别的内容,<em>只</em>出现在你的博客的首页上?对,只出现在首页或者主页上,在你的站点的其它位置上看不到这些内容。简单!我们称这个为''静态首页''。你的站点的首页或者第一个网页并不是真的是静态的。只是使用Loop使首页看起来是那样的。


To make this Loop trick work, use the [[WordPress:Conditional_Tags#The_Main_Page|is_home()]] conditional template tag function.
To make this Loop trick work, use the [[WordPress:Conditional_Tags#The_Main_Page|is_home()]] conditional template tag function.
要使得这个Loop trick运行,使用[[WordPress:Conditional_Tags#The_Main_Page|is_home()]]条件模板标签函数。


In your <tt>index.php</tt>, use an <tt>if ()</tt> test to conditionally output additional content:
In your <tt>index.php</tt>, use an <tt>if ()</tt> test to conditionally output additional content:
在你的<tt>index.php</tt>,使用一个<tt>if ()</tt>测试来有条件地输出额外的内容:


<pre>
<pre>
第301行: 第764行:
} ?&gt;  
} ?&gt;  
</pre>
</pre>
<pre>
&lt;?php get_header(); ?&gt;
&lt;?php if (is_home()) {
// 我们在主页上,因此让我们来秀秀我们新的小猫的图片!
echo "&lt;img src='/images/new_kitty.jpg' alt='Our new cat, Rufus!' /&gt;";
//现在返回到我们正常组织的主页
} ?&gt;
</pre>


The function <tt>is_home()</tt> will only produce a true value if the visitor is not requesting a specific post, page, category, or date, so it only shows up on the "home" page.
The function <tt>is_home()</tt> will only produce a true value if the visitor is not requesting a specific post, page, category, or date, so it only shows up on the "home" page.
如果访客没有要求一个特别的文章,网页,类别,或者日期,函数<tt>is_home()</tt>只会产生一个真值,因此它只会显示在"主页" 上。


For more information, see [[WordPress:Creating a Static Front Page]].
For more information, see [[WordPress:Creating a Static Front Page]].
更多的信息,请看[[WordPress:Creating a Static Front Page|创建一个静态首页]]。


===Excerpts Only===
===Excerpts Only===
===只有摘录===
The easiest way to display excerpts, instead of the full content, of posts, replace all instances of <tt>[[WordPress:Template_Tags/the_content|the_content]]()</tt> with <tt>[[WordPress:Template_Tags/the_excerpt|the_excerpt()]]</tt>.  If you have not created explicit excerpts for your posts, this function will automatically display the first 120 words of the post.
The easiest way to display excerpts, instead of the full content, of posts, replace all instances of <tt>[[WordPress:Template_Tags/the_content|the_content]]()</tt> with <tt>[[WordPress:Template_Tags/the_excerpt|the_excerpt()]]</tt>.  If you have not created explicit excerpts for your posts, this function will automatically display the first 120 words of the post.
显示摘录而不是文章所有内容的最简单的方法是,用<tt>[[WordPress:Template_Tags/the_excerpt|the_摘录()]]</tt>来取代所有的<tt>[[WordPress:Template_Tags/the_content|the_内容]]()</tt>的例子。如果你没有为你的文章创建摘录,这个函数会自动地显示文章的前120个词。
<pre>
<pre>
&lt;div class="entry"&gt;
&lt;div class="entry"&gt;
第313行: 第800行:
&lt;/div&gt;
&lt;/div&gt;
</pre>
</pre>
<pre>
&lt;div class="entry"&gt;
&lt;?php the_excerpt(); ?&gt;
&lt;/div&gt;
</pre>


===Showing Excerpts or Full Post Depending Upon Number of Posts===
===Showing Excerpts or Full Post Depending Upon Number of Posts===
===根据文章的数目来显示文章摘录或者全部的文章内容===
In some circumstances, for example on archive pages, you may want to show the full post if there is only one post or excerpts if there are multiple posts. You can customize the loop to do this.
In some circumstances, for example on archive pages, you may want to show the full post if there is only one post or excerpts if there are multiple posts. You can customize the loop to do this.
在有的情况下,例如,在文章档案网页中,如果只有一篇文章,你可能想要显示全部的文章内容,如果有几篇文章,你可能只想显示摘录部分。你可以自定义loop来显示全部内容或者摘录。


<pre>
<pre>
第325行: 第829行:
           <?php the_excerpt() ?>
           <?php the_excerpt() ?>
       <!-- Do your post footer stuff here for excerpts-->
       <!-- Do your post footer stuff here for excerpts-->
    <?php endwhile; ?>
  <?php else : ?>
    <?php while (have_posts()) : the_post(); ?>
      <!-- Do your post header stuff here for single post-->
          <?php the_content() ?>
      <!-- Do your post footer stuff here for single post-->
    <?php endwhile; ?>
  <?php endif; ?>
<?php else : ?>
    <!-- Stuff to do if there are no posts-->
<?php endif; ?>
</pre>
<pre>
<?php if (have_posts()) : ?>
  <?php if (($wp_query->post_count) > 1) : ?>
    <?php while (have_posts()) : the_post(); ?>
      <!—你在这儿为摘录粘贴页眉吗-->
          <?php the_excerpt() ?>
      <!—你在这儿为摘录粘贴页脚吗-->
     <?php endwhile; ?>
     <?php endwhile; ?>


第345行: 第881行:


===Different Headers/Sidebars/Footers===
===Different Headers/Sidebars/Footers===
===不同的页眉/工具条/页脚===
WordPress offers the <tt>get_header()</tt>, <tt>get_sidebar()</tt>, and <tt>get_footer()</tt> [[WordPress:Include Tags]] for use in your [[WordPress:Templates|template files]].  These functions make it easy to define a standard header/sidebar/footer which is easily editable.  Any changes made to these files will immediately be made visible to viewers, without any work on your part.
WordPress offers the <tt>get_header()</tt>, <tt>get_sidebar()</tt>, and <tt>get_footer()</tt> [[WordPress:Include Tags]] for use in your [[WordPress:Templates|template files]].  These functions make it easy to define a standard header/sidebar/footer which is easily editable.  Any changes made to these files will immediately be made visible to viewers, without any work on your part.
WordPress在你的[[WordPress:Templates|模板文件]]中提供使用<tt>get_header()</tt>, <tt>get_sidebar()</tt>, 和 <tt>get_footer()</tt> [[WordPress:Include Tags|包括标签]]。这些函数使得定义一个标准的页眉/工具条/页脚变得简单,一个标准的页眉/工具条/页脚编辑简单。访客能够立马地看到这些文件中发生的任何变化,不包括你所执行的内容。


But sometimes you might not <em>want</em> a sidebar.  If you don't want a sidebar, simply exclude the call to the <tt>get_sidebar()</tt> function from your template.  For example, the <tt>single.php</tt> template in the WordPress default theme does not include a sidebar.
But sometimes you might not <em>want</em> a sidebar.  If you don't want a sidebar, simply exclude the call to the <tt>get_sidebar()</tt> function from your template.  For example, the <tt>single.php</tt> template in the WordPress default theme does not include a sidebar.
但是有时候,你可能不<em>想要/em>一个工具条。如果你不想要一个工具条,只要移除调用到你的模板的<tt>get_sidebar()</tt>函数。例如,WordPress默认主题的<tt>single.php</tt>模板不包括一个工具条。


To create your own <strong>different</strong> sidebar, you have two choices.
To create your own <strong>different</strong> sidebar, you have two choices.
要创建你自己的<strong>不同的</strong>工具条,你拥有两个选择。
# Include the sidebar contents directly into the template file on which you're working.  If you want category-3 to have a different sidebar, edit <tt>category-3.php</tt> and include the necessary HTML and PHP to generate your distinctive sidebar.
# Include the sidebar contents directly into the template file on which you're working.  If you want category-3 to have a different sidebar, edit <tt>category-3.php</tt> and include the necessary HTML and PHP to generate your distinctive sidebar.
#将工具条内容直接包含到你正在处理的模板文件中。如果你想要类别-3拥有一个不同的工具条,编辑<tt>类别-3.php</tt>并且包含必须的HTML和PHP来产生你的独特的工具条。
# Use the PHP <tt>[http://www.php.net/include include]</tt> function, to include another file.  The WordPress <tt>get_sidebar()</tt> function <em>only</em> loads <tt>sidebar.php</tt>.  If you make a file named <tt>sideleft.php</tt>, you would include it like this:
# Use the PHP <tt>[http://www.php.net/include include]</tt> function, to include another file.  The WordPress <tt>get_sidebar()</tt> function <em>only</em> loads <tt>sidebar.php</tt>.  If you make a file named <tt>sideleft.php</tt>, you would include it like this:
#使用PHP<tt>[http://www.php.net/include 包括]</tt>函数来包括另一个文件。WordPress<tt>get_sidebar()</tt> 函数<em>only</em> 载入<tt>sidebar.php</tt>。如果你制作了一个文件,命名为<tt>sideleft.php</tt>,你可以像这样将它包含:
<pre>
<pre>
&lt;?php include(TEMPLATEPATH . '/sideleft.php'); ?&gt;
&lt;?php include(TEMPLATEPATH . '/sideleft.php'); ?&gt;
</pre>
</pre>
<pre>
&lt;?php include(TEMPLATEPATH . '/sideleft.php'); ?&gt;
</pre>


Using the WordPress default [[WordPress:Template Hierarchy]], if you want to use the same elements on multiple or different templates, it's probably best to put them in separate template files and use the PHP <tt>include()</tt> function.  If the element you're adding is specifically for one template file, it's probably best to include it directly in that template file.
Using the WordPress default [[WordPress:Template Hierarchy]], if you want to use the same elements on multiple or different templates, it's probably best to put them in separate template files and use the PHP <tt>include()</tt> function.  If the element you're adding is specifically for one template file, it's probably best to include it directly in that template file.
使用WordPress默认的[[WordPress:Template Hierarchy|模板层级]],如果你想在多个或者不同的模板中使用相同的元素,最好将这些元素放进分开的模板文件中并且使用PHP <tt>include()</tt>函数。如果你添加的元素是专门为一个模板文件准备的,那么你最好直接将这个元素包含在那个模板文件中。


==Summary==
==Summary==
==总结==
We've just scratched the surface of what can be done with the Loop.  As a reminder, the following are resources that will help you customize your own [[WordPress:The Loop|WordPress Loop]].
We've just scratched the surface of what can be done with the Loop.  As a reminder, the following are resources that will help you customize your own [[WordPress:The Loop|WordPress Loop]].
我们刚刚初步了解了Loop的用法。作为一个提醒,下面的资源会帮助你自定义你自己的[[WordPress:The Loop|WordPress Loop]]。


* [[WordPress:Templates|Template Files]]
* [[WordPress:Templates|Template Files]]
第369行: 第936行:
[[WordPress:Category:Design and Layout]]
[[WordPress:Category:Design and Layout]]
[[WordPress:Category:Advanced Topics]]
[[WordPress:Category:Advanced Topics]]
* [[WordPress:Templates|模板文件]]
* [[WordPress:Template Tags|模板标签]]
* [[WordPress:Template Hierarchy|模板层级]]
* [[WordPress:Conditional Tags|条件式标签]]
[[WordPress:Category:WordPress Lessons|类别:WordPress课程]]
[[WordPress:Category:Design and Layout|类别:设计和布局]]
[[WordPress:Category:Advanced Topics|类别:高级主题]]


==Resources==
==Resources==
==资源==
* [http://www.obeattie.com/2006/05/02/wordpress-the-loop/ oBeattie : The Loop]
* [http://www.obeattie.com/2006/05/02/wordpress-the-loop/ oBeattie : The Loop]
* [http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/ The Ultimate Guide to the WordPress Loop]
* [http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/ The Ultimate Guide to the WordPress Loop]
* [http://www.obeattie.com/2006/05/02/wordpress-the-loop/ oBeattie : The Loop]
* [http://www.themelab.com/2008/04/04/the-ultimate-guide-to-the-wordpress-loop/ WordPress Loop的根本指导]

2008年6月5日 (四) 17:19的版本

Introduction

介绍

"The Loop" is a term that refers to the main process of WordPress. You use The Loop in your template files to show posts to visitors. You could make templates without The Loop, but you'd only be able to display data from one post.

"The Loop"是一个术语,指的是WordPress的主要程序。你在模板文件中使用Loop,来想你的访客显示文章。你可以不用Loop,来制作模板,但是你只能从一篇文章中显示数据。

The first thing WordPress does is check that all the files it needs are present. Next, it collects the default settings, as defined by the blog administrator, from the database. This includes things like the number of posts to display per page, whether commenting is enabled, and the like. Once these defaults are established, WordPress checks to see what the user asked for. This information is used to determine which posts to fetch from the database.

WordPress首先要做的就是检查一下,它所需要的文件都已经呈现了。然后,WordPress收集一下,数据库中的博客 管理员所定义的默认设置。这些默认设置包括,每个网页应该显示多少篇文章,是否允许评论,等等。一旦这些默认设置建立了,WordPress便会查看用户需要什么。用户的需求信息会用来决定从数据库中提取那篇文章。


If the user didn't ask for a specific post, category, page, or date, WordPress uses the previously collected default values to determine which posts to prepare for the user. For example, if the blog administrator has selected to display 5 posts per page in Administration > Settings > Reading, then WordPress will fetch the five most recent posts from the database. If the user did ask for a specific post, category, page, or date, then WordPress will use that information to specify which post(s) to fetch from the database.

如果用户没有要求要一篇特别的文章,类别,网页,或者日期,WordPress使用先前收集的默认值来决定为用户呈现那篇文章。例如,如果博客管理员选择在管理 > 设置 > 阅读中的每个网页中显示五篇文章,那么WordPress就会在数据库中提取五篇最新的文章。如果用户的确要求要一篇特别的文章,类别,网页,或者日期,那么WordPress会使用这个信息来决定从数据库中提取哪一篇(哪些)文章

Once all this is done, WordPress connects to the database, retrieves the specified information, and stores the results in a variable. It is The Loop that accesses this variable, and uses the values for display in your templates.

一旦上述的任务完成了,WordPress就会连接到数据库,重新得到特别的信息,并且用一个变数来储存结果。这是Loop能够使用这个变数,并且使用这个值来显示在你的模板上。

By default, if the visitor did not select a specific post, page, category, or date, WordPress uses index.php to display everything. For the first part of this discussion of The Loop we'll focus only on index.php, and the default display of your blog. Later on, once you understand how things work, we'll investigate tricks with The Loop in other template files.

默认情况下,如果访客没有选择一篇特别的文章,网页,类别,或者日期,WordPress使用index.php来显示所有的内容。对于探讨Loop的第一部分,我们只突出index.php,和你的博客的默认显示。然后,你一旦了解了这些内容是怎样运行的,我们就会研究Loop在其它的模板文件中的一些窍门。

The World's Simplest Index Page

世界上最简单的搜索页

The following is a fully functional index which will display the contents (and just the contents) of each post, according to the conditions used to prepare The Loop. The only purpose for showing you this is to demonstrate how little is actually necessary for the functioning of The Loop. The bulk of the stuff in your index.php is CSS, HTML, and PHP declarations to make The Loop look pretty.

下面是一个完全性的功能性的索引,这个索引根据用来准备Loop的条件,显示了每篇文章的内容(仅仅是内容)。向你显示这个索引的唯一目的就是,要证明Loop功能运转不怎么必要。你的index.php中内容的大部分是,CSS, HTML,和PHP声明来使得Loop看起来更漂亮。

<?php
get_header();
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_content();
   endwhile;
endif;
get_sidebar();
get_footer(); 
?>


<?php
get_header();
if (have_posts()) :
   while (have_posts()) :
      the_post();
      the_content();
   endwhile;
endif;
get_sidebar();
get_footer(); 
?>


Now, let's look at the bulk of the stuff that makes The Loop look pretty.

现在,让我们来看看这些使得Loop看起来更好看的部分。

The Default Loop

默认的Loop

The following is a step-by-step look at the default usage of the Loop that comes with the default and classic theme in the standard installation of WordPress v1.5.

下面内容是关于按部就班地查看Loop的使用,这个Loop伴随在WordPress1.5版本的标准安装的默认经典主题。

Begin The Loop

开始 Loop

Found at the top of the default index.php template file is the starting code for WordPress:The Loop.

在默认index.php模板文件的顶端是The Loop的开始代码。

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


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


  1. First it checks whether any posts were collected with the have_posts() function.
  1. 首先它检查了文章是不是用这个have_posts()函数收集了。
  1. If there are any posts, a PHP while loop is started. A while loop will continue to execute as long as the condition in the parenthesis is logically true. So as long as the function have_posts() returns a true value, The Loop will keep going.
  1. 如果有文章,一个PHPwhile loop就会开始运转。只要括号中的条件在逻辑上是真的,那么一个while loop就会继续执行。只要函数have_posts()返回了一个真值,Loop就会继续运行。
  1. The function have_posts() simply checks the next item in the collection of posts: if there's another item, return true; if there is no next item, return false.
  1. 函数have_posts()在文章收集的时候,只会检查下一篇文章:如果有下一篇文章,就会返回真;如果没有下一篇文章,就会返回假。

Generating the Post

产生文章

The function the_post() takes the current item in the collection of posts and makes it available for use inside this iteration of The Loop. Without the_post(), many of the WordPress:Template Tags used in your theme would not work.

Once the post data is made available, the template can start showing post data to the visitor.

函数the_post()在收集文章的时候,拿着当前的文章,并且使得当前的文章在Loop的反复中能够使用。没有了the_post(),你的主题中使用的许多的模板标签都不能运行。

Title, Date and Author

标题,日期和作者

The following template tags get the current post's title, as well as the time it was posted and who posted it.

下面的模板标签得到了当前文章的标题,文章发表的时间,以及谁发表了这篇文章。

<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>


<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

Post Content

文章内容

The the_content() template tag displays the content of the post. This is the meat and potatoes of each pass through The Loop:

the_内容()模板标签显示了文章的内容。这是通过Loop的每篇文章的最重要的部分。

<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>



<div class="entry">
<?php the_content('Read the rest of this entry &raquo;'); ?>
</div>


If you include the Quicktag button called more, and shown as <!--more-->, in the body of your post, only the portion above that line will be displayed to viewers. So, if you only want your front page to show the first sentence or two of every post, simply insert <!--more--> after the first line into every post you make.

如果你包含了称作更多的Quicktag按钮,而且这个按钮在你的文章主体显示为<!--more-->,只有那一行上面的部分才会显示给访客。因此,如果你只想首页显示每篇文章的第一句话,或者前两句话,只要在你所写的文章中的第一行后插入<!--more-->,就可以了。


When viewing a single post, the <!--more--> delimiter is skipped. So putting the <!--more--> delimiter into all your posts forces readers to click through to each individual post if they want to read the whole thing.

当你阅读一篇单一的文章的时候,<!--more-->分隔符就被跳过了。因此在你所有的文章中放入<!--more-->分隔符,如果读者想阅读所有的内容的话,这个分隔符会迫使读者来点击每篇单个的文章。

Additional Details

额外的详细信息

Beneath each post's content in the index.php template file is a place to display more information about the post, such as the categories, date, and comment information. Known as the post meta data section, if you're a logged in user of sufficient privilege (or the post's author), you will also see an "Edit This" link, thanks to the edit_post_link() template tag function.

index.php模板文件中的每篇文章的内容下面,有一个位置可以显示文章的更多的信息,例如类别,数据,评论信息。称作文章 meta 数据 部分,如果你是一个注册过的用户,拥有足够的特权(或者文章的作者),你就会看见一个"编辑这个"链接,感谢edit_post_link() 模板标签函数。

<p class="postmetadata">
Posted in <?php the_category(', ') ?> 
<strong>|</strong>
<?php edit_post_link('Edit','','<strong>|</strong>'); ?>  
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>


<p class="postmetadata">
Posted in <?php the_category(', ') ?> 
<strong>|</strong>
<?php edit_post_link('Edit','','<strong>|</strong>'); ?>  
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>


If commenting is enabled, or if the post has comments, the comments_popup_link() template tag will display a link to the comments. If you're using the comments popup window, this link will open the comments window; otherwise it will jump right to this post's comments.

如果写评论得到了激活,或者如果文章拥有评论,comments_popup_link()模板标签就会显示一个连接到评论的链接。如果你正在使用评论弹出窗口,这个链接会打开评论窗口,否则的话,它就会跳到这篇关于这篇文章的评论。

If the visitor is viewing an index of posts (i.e.: more than one post in The Loop), the comments_popup_link() link will take the reader to this post's individual page.

如果访客正在访问文章的索引(例如: Loop中有几篇文章), comments_popup_link()链接会将读者带到这篇文章的单个的网页。

Trackback Autodiscovery

Trackback 自动查找

The trackback_rdf template tag's function is to output machine-readable code used for trackback auto-discovery.

trackback_rdf模板标签的功能是要产出用于trackback自动查找的,机器能够识别的代码。

<!--
<?php trackback_rdf(); ?>
-->



<!--
<?php trackback_rdf(); ?>
-->


Note: The trackback_rdf() tag is supposed to be used with comments around it. It is not "turned off".

注: trackback_rdf()标签使用时,周围应该有评论

Ending The Loop

编辑Loop

The following ends The Loop. After this, the various post-related template tags will not work as expected (or if they do, they will use the last post from The Loop). This means, that if you need to use a template tag that works within The Loop, you need to put it in before this point.

以下的函数结束了Loop。这个函数执行之后,不同的与文章相关的模板标签,就不会像期望地那样运行(或者如果这些模板运行的话,它们会使用Loop中的最后一篇文章)。这意味着,如果你要使用一个模板标签,这个标签在 Loop内部运行,你就要将这个标签放在这一点之前。

<?php endwhile; ?>
<?php endwhile; ?>


This section, immediately after the end of The Loop, displays navigation controls to move forward and backward by each web page.

这个部分,就在结束Loop的后面,显示了通过每个网页而前进或者后退的导航控制。

<div class="navigation">
<div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
<div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
</div>


<div class="navigation">
<div class="alignleft"><?php posts_nav_link('','','&laquo; Previous Entries') ?></div>
<div class="alignright"><?php posts_nav_link('','Next Entries &raquo;','') ?></div>
</div>

If the blog is set to display 10 posts per page, and the conditions used by The Loop collect 25 posts, there will be three pages to navigate: two pages of 10 posts each, and one page of 5 posts. The navigation links will allow the visitor to move forward and backward through the collection of posts.

如果博客被设置为每个网页显示十篇文章,而且Loop使用的条件收集了25篇文章,那么会有三个网页需要导航:其中两个网页,每个网页有十篇文章,还有一个网页有五篇文章。导航链接能够使访客在收集的文章中,向前或者向后翻看。

The navigation controls are included outside The Loop, but inside the if condition, so that they only show up if there are any posts. The navigation functions themselves also check whether or not there is anything to which they will link, based on the current Loop, and only display links if there's something to link.

导航控制包含在Loop中,但是包含假设条件中,因此只有有文章的时候,导航控制才会出现。导航函数本身也会以当前的Loop为基础,查看有没有它们可以链接的内容,而且只有在有一些可以链接的内容的情况下,导航函数才会显示链接。

<?php else : ?>
 <h2 class="center">Not Found</h2>
 <p class="center">
<?php _e("Sorry, but you are looking for something that isn't here."); ?></p>



<?php else : ?>
 <h2 class="center">Not Found</h2>
 <p class="center">
<?php _e("Sorry, but you are looking for something that isn't here."); ?></p>




The else : clause determines what to do if have_posts() (from way up at the top) is false. That is to say, the stuff after the else will only be executed/displayed if The Loop had zero posts. No posts show up if, for example, the visitor requested a specific day for which no posts were made or a search was performed that produced no results.

其它的 :字句决定了如果有_文章()(在顶端上方)是假的,应该执行什么。这就是说,只有Loop中没有文章的时候,其它的后面的内容才会得到执行/显示。例如,如果访客需要某天的文章,但是这一天没有文章,或者经过了搜索,但是没有产生结果,没有显示文章。


  <?php endif; ?>


  <?php endif; ?>


This ends the conditional test of "if there are posts do this, else if there are no posts, do that". Once the conditional test is finished, the default index.php template next includes the sidebar, and finally the footer.

这个结束了条件测试"如果有文章,这样执行,如果没有文章,那样执行"。一旦条件测试结束了,默认的index.php模板然后包含工具条,最后包含页脚。

The Loop In Other Templates

其它的模板中的Loop

WordPress can use different template files for displaying your blog in different ways. In the default WordPress theme, there are template files for the index view, category view, and archive view, as well as a template for viewing individual posts. Each of these uses WordPress:The Loop, but does so with slightly different formatting, as well as different uses of the template tags.

WordPress可以使用不同的模板文件以不同的方式来显示你的博客。在WordPress默认主题中,有关于索引浏览,类别浏览和归档浏览的模板文件,也有一个浏览个人文章的模板。这些模板和模板文件都使用WordPress:The Loop,但是使用的格式有所不同,而且对模板标签的使用也有所不同。

For any view which does not have a separate template file, WordPress will use index.php by default. If a visitor requests a single post, WordPress will first look for a file named single.php. If that file exists, it will be used to present the post to the visitor. If that file does not exist, WordPress will use index.php to present the post to the visitor. This is called the WordPress:Template Hierarchy.

对于没有一个单独的模板文件的浏览,WordPress会默认使用index.php。如果一位访客需要一篇单一的文章,WordPress会首先查看一个名称为single.php的文件。如果这个文件存在的话,这个文件就会被用来向访客呈现文章。如果这个文件不存在,WordPress会使用index.php来向访客呈现文章。这叫做模板层级

If you are making your own Theme, it's often helpful to look at the template files from the default Theme as a point of reference. It's also helpful to use your theme's index.php as a template for your other template files. Doing so may give you a known and working page from which to begin making changes as you create more template files.

如果你正制作你自己的主题,看看默认主题中作为参考的模板文件是很有用的。为你的其它模板文件使用你的主题的index.php作为一个模板,也很有用。执行了这些之后,你可能得到了一个已知的,并且正在运行的网页,当你创建更多的模板文件的时候,这个网页就会发生变化。

Different Archive Format

不同的归档样式

An archive is a collection of historical posts. In the default usage, the posts displayed on your main index are recent chronological postings. When a visitor clicks on one of your archive links, or if they manually request a specific date (http://www.example.com/blog/index.php?m=200504 or http://www.example.com/blog/2005/04 to select all posts from April, 2005), WordPress will display an archive view. By default, the archive will use index.php, and thus look the same as your front page, just displaying the posts from April 2005.

一个归档是对过去的文章的收集。在默认使用中,显示在你的主索引上的文章是最近按时间顺序的发表的文章。当访客点击你其中的一个归档链接的时候,或者如果访客手工操作要求一个特备的日期(http://www.example.com/blog/index.php?m=200504 or http://www.example.com/blog/2005/04 选择从2005年四月开始的所有的文章),WordPress会显示一个档案 浏览。默认情况下,档案会使用index.php,因此档案看起来和你的首页是相同的,只显示从2005年四月开始的文章。

When WordPress prepares an archive view for a visitor, it specifically looks for a file named archive.php in your current theme's directory. If you'd like to visually disambiguate archives from your front page, simply copy index.php to archive.php, and edit archive.php as necessary!

当WordPress为一名访客准备一个档案浏览的时候,WordPress特别要查找你当前主题目录中一个名为archive.php的文件。如果你想真实地消除首页中的档案的歧义,你只要复制index.phparchive.php,并且需要的话,编辑archive.php

For example, if you want to show only post titles, and no post content, for your list of archives, you could use something like this:

例如,如果你只想在你的文章档案列表上显示文章标题,而不是文章内容,你可能要使用以下的内容:

<?php get_header(); ?>
 <div id="content" class="narrowcolumn">

  <?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
     <div class="post">
     <h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
     <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
      </div>
    <?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','&laquo; Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries &raquo;','') ?>
</div>
  </div>
<?php else : ?>
  <h2 class="center">Not Found</h2>
 <p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
  <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>





<?php get_header(); ?>
 <div id="content" class="narrowcolumn">

  <?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
     <div class="post">
     <h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
     <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
      </div>
    <?php endwhile; ?>
<div class="navigation">
<div class="alignleft">
<?php posts_nav_link('','','&laquo; Previous Entries') ?>
</div>
<div class="alignright">
<?php posts_nav_link('','Next Entries &raquo;','') ?>
</div>
  </div>
<?php else : ?>
  <h2 class="center">Not Found</h2>
 <p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
  <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>




Different Category Format

不同的类别样式

Like the archive views, WordPress looks for a separate template file for category views. If a visitor clicks on a link for a category in your blog, they will be taken to the category view. WordPress will prepare The Loop with posts from that category only, limiting the number of posts per the blog's default settings.

与档案浏览相似,WordPress为类别浏览查找一个单独的模板文件。如果一个访客点击你的博客上一个类别的一个链接,访客就会来到类别浏览。WordPress会给Loop准备来自那个类别的文章,并且根据博客的默认设置来限制文章的数目。

To make your category view different from your index view, copy index.php and rename it category.php. For a category view, it's probably not necessary to list the categories to which a post is assigned, so let's remove that portion. Instead, let's announce the category at the top of the page:

要使你的类别浏览与你的索引浏览有所区别,复制index.php并且将它重新命名为category.php。对于一个类别浏览,可能没有必要列出文章所在的类别,因此我们移除这个部分。反之,让我们在网页的顶上方公布类别:

<?php get_header(); ?>
 <div id="content" class="narrowcolumn">
 <p>
 <strong>
  <?php single_cat_title('Currently browsing '); ?>
  </strong><br />
 <?php echo category_description(); ?>
 </p>
 <?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
     <div class="post">
      <h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
   <small>
     <?php the_time('F jS, Y') ?> 
        <!-- by <?php the_author() ?> -->
   </small>
 </div>
<?php endwhile; ?>
 <div class="navigation">
   <div class="alignleft">
    <?php posts_nav_link('','','&laquo; Previous Entries') ?>
   </div>
   <div class="alignright">
    <?php posts_nav_link('','Next Entries &raquo;','') ?>
   </div>
 </div>
<?php else : ?>
  <h2 class="center">Not Found</h2>
<p class="center"><?php _e("Sorry, but you are looking for something that isn't here."); ?></p>
 <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>





<?php get_header(); ?>
 <div id="content" class="narrowcolumn">
 <p>
 <strong>
  <?php single_cat_title('Currently browsing '); ?>
  </strong><br />
 <?php echo category_description(); ?>
 </p>
 <?php if (have_posts()) : ?>
   <?php while (have_posts()) : the_post(); ?>
     <div class="post">
      <h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
   <small>
     <?php the_time('F jS, Y') ?> 
        <!-- by <?php the_author() ?> -->
   </small>
 </div>
<?php endwhile; ?>
 <div class="navigation">
   <div class="alignleft">
    <?php posts_nav_link('','','&laquo; Previous Entries') ?>
   </div>
   <div class="alignright">
    <?php posts_nav_link('','Next Entries &raquo;','') ?>
   </div>
 </div>
<?php else : ?>
  <h2 class="center">Not Found</h2>
<p class="center"><?php _e("对不起, 但是你找的东西,不在这儿。"); ?></p>
 <?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>



Different Formats for Different Categories

不同类别的不同样式

As explained in the WordPress:Template Hierarchy, it is possible to create separate template files for each category. Simply name the file category-X.php, where X is the numerical ID of the category. Consider carefully whether you need a whole new template for a specific category.

就如模板层级里所解释的那样, 为每个类别创建单独的模板文件时可能的。只要命名文件category-X.php,其中X是类别的数字ID。认真考虑你是否需要为一个特别的类别准备一个全新的模板。

Let's look at two categories, "Plants" and "Flowers", with category IDs 3 and 4, respectively. Next to each post title in the output you want to have picture of either a plant, or a flower, depending on which category is being displayed. You could:

让我们来看看两个类别,"植物"和 "花",各自的类别ID是3和4。在每个文章标题旁边的输出中,取决于你正在显示的类别,你可能想要一颗植物或者一朵花的图片。你可以:

  • Use two separate files, category-3.php and category-4.php, each with a different img tag for each post title.
  • 使用两个单独的文件,类别-3.php类别-4.php,每个类别中每篇文章的标题都有一个不同的img标签。
  • Use a conditional test inside your default category.php file to check whether the current category is "Plants" or "Flowers" (or neither), and display the appropriate image:
  • 在你的默认的category.php文件中使用一个条件测试来看看当前的类别是"植物" 还是 "花朵"(或者都不是),并且显示适当的图像:
<?php if (is_category('3') ):
 // we're in the Plants category, so show a plant ?>
 <img src='/images/plant.png' alt='a plant' />
<?php } elseif (is_category('4') ):
 // we're in the Flowers category, so show a flower ?>
 <img src='/images/flower.png' alt='a pretty flower' />
<?php endif; // end the if, no images for other other categories ?>


<?php if (is_category('3') ):
 // 我们在植物类别,因此显示一颗植物 ?>
 <img src='/images/plant.png' alt='a plant' />
<?php } elseif (is_category('4') ):
 // 我们在花朵类别,因此显示一朵花?>
 <img src='/images/flower.png' alt='a pretty flower' />
<?php endif; // end the if, no images for other other categories ?>

If you added another category, "Cars", which you wanted to display in a significantly different way, then a separate category-X.php would be more appropriate.

如果你添加了另一个类别,"汽车",你想要以非常不同的方式来显示这个类别,那么一个分开的类别-X.php会更加地合适。

Different CSS For Different Categories

不同类别的不同的CSS

Many users want to create separate CSS files for a specific category. This, too, can be easily accomplished. It is important to remember that stylesheets are defined and loaded in the <head> section of the HTML document. WordPress uses the header.php file for this. In the default header.php, find this line:

许多用户想要为一个特别的类别创建单独的CSS文件。这一点也很容易实现。记住样式表是在HTML文件的<head>中定义和载入的,这一点很重要。WordPress可以为这个使用header.php文件。在默认header.php,找到这一行:

<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />


<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />

And change it to something like this: 将这个改为:

<?php if ( is_category('5') ) { // Load special CSS for "Cars" category ?>
  <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/category-5.css" type="text/css" media="screen" />;
<?php } else { ?>
   <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php } ?>


<?php if ( is_category('5') ) { // Load special CSS for "Cars" category ?>
  <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/category-5.css" type="text/css" media="screen" />;
<?php } else { ?>
   <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php } ?>



Note: The Cars template uses the category-5.css file to override the default layout. In this example the CSS file is named after the category template file to which it will be applied, rather than the actual name of the category. Thus, you know that category-5.css goes with category-5.php.

注:汽车模板使用类别-5.css文件来覆盖默认设置。在这个例子中,CSS文件命名在类别模板文件之后,而不是正真的类别名,CSS文件将会用在类别模板文件中。因此,你知道category-5.csscategory-5.php在一起。

Different Single Post Format

不同的单个文章格式

When viewing any single post (or permalink), WordPress will use single.php, if present.

当访问任何单一的文章(或者permalink),WordPress会使用single.php

This portion, from the WordPress default single.php, provides the post meta data information about the current post:

来自WordPress默认single.php的这个部分,提供了关于当前文章的文章 meta 数据 信息

<p class="postmetadata alt">
<small>

<pre>
<p class="postmetadata alt">
<small>


This entry was posted on <?php the_time('l, F jS, Y') ?> at <?php the_time() ?> 
and is filed under <?php the_category(', ') ?>.
You can follow any responses to this entry through 
the <?php comments_rss_link('RSS 2.0'); ?> feed.

这篇文章发表在<?php the_time('l, F jS, Y') ?>在 <?php the_time() ?>而且归档在<?php the_category(', ') ?>。通过<?php comments_rss_link('RSS 2.0'); ?> feed,你可以遵循任何对于这篇文章的响应。

<?php
if (('open' == $post->comment_status) && ('open' == $post->ping_status)) {
// Both Comments and Pings are open
?>
  You can <a href="#respond">leave a response</a>, or 
  <a href="<?php trackback_url(display); ?>">trackback</a> 
from your own site.
<?php 
} elseif (!('open' == $post->comment_status) && ('open' == $post->ping_status)) {
// Only Pings are Open 
?>
  Responses are currently closed, but you can 
  <a href="<?php trackback_url(display); ?> ">trackback</a> 
from your own site.
<?php
} elseif (('open' == $post->comment_status) && !('open' == $post->ping_status)) { 
// Comments are open, Pings are not 
?>
  You can skip to the end and leave a response. Pinging is currently not allowed.
<?php
} elseif (!('open' == $post->comment_status) && !('open' == $post->ping_status)) { 
// Neither Comments, nor Pings are open 
?>
  Both comments and pings are currently closed.
<?php 
} 
edit_post_link('Edit this entry.','',''); ?>
</small>
</p>







<?php if (('open' == $post->comment_status) && ('open' == $post->ping_status)) { // 评论和 Pings都打开了 ?>

 你可以 <a href="#respond">leave a response</a>,或者
 <a href="<?php trackback_url(display); ?>">trackback</a> 

来自你自己的站点。 <?php } elseif (!('open' == $post->comment_status) && ('open' == $post->ping_status)) { // 只有 Pings 是打开的。 ?>

 回响当前关闭了,但是你可以从你自己的站点上  <a href="<?php trackback_url(display); ?> ">trackback</a> 

。 <?php } elseif (('open' == $post->comment_status) && !('open' == $post->ping_status)) { // 评论是打开的,但是Ping没有。 ?>

 你可以跳读到最后,并且留下一个响应。Ping当前不被允许。<?php

} elseif (!('open' == $post->comment_status) && !('open' == $post->ping_status)) { // 评论和Ping都没有打开 ?>

 评论和pings当前都关闭了。<?php 

} edit_post_link('编辑这篇文章。',,); ?> </small> </p>





This sort of information -- whether comments are open or closed -- is largely inappropriate on an index, archive, or category view; which is why it's only included in the single.php template file.

这种形式的信息—不过评论是打开的还是关闭的—对于一个索引,归档,或者类别浏览是不合适的;这就是这种信息只包含在single.php模板文件中的原因。

Other Loop Tricks

其它的 Loop Tricks

Now that you have a good introduction to the basic uses for the WordPress Loop, let's introduce you to some more Loop effects and tricks.

既然对于WordPress Loop的基本用户,你有一个好的介绍说明。让我们给你引入更多的Loop effects和tricks。

Static Front Page

静态的首页

How can you display something special only on the front page of your blog? That's right, only on the front page or home page, and have it not be seen anywhere else on your site. Easy! We call this the static front page. The front or first page of your site isn't really static. It's just using the Loop to make it look that way.

你怎样显示一些特别的内容,出现在你的博客的首页上?对,只出现在首页或者主页上,在你的站点的其它位置上看不到这些内容。简单!我们称这个为静态首页。你的站点的首页或者第一个网页并不是真的是静态的。只是使用Loop使首页看起来是那样的。

To make this Loop trick work, use the is_home() conditional template tag function.

要使得这个Loop trick运行,使用is_home()条件模板标签函数。

In your index.php, use an if () test to conditionally output additional content:

在你的index.php,使用一个if ()测试来有条件地输出额外的内容:

<?php get_header(); ?>
<?php if (is_home()) {
 // we're on the home page, so let's show a picture of our new kitten!
 echo "<img src='/images/new_kitty.jpg' alt='Our new cat, Rufus!' />";
 // and now back to our regularly scheduled home page
} ?> 


<?php get_header(); ?>
<?php if (is_home()) {
 // 我们在主页上,因此让我们来秀秀我们新的小猫的图片!
 echo "<img src='/images/new_kitty.jpg' alt='Our new cat, Rufus!' />";
 //现在返回到我们正常组织的主页 
} ?> 


The function is_home() will only produce a true value if the visitor is not requesting a specific post, page, category, or date, so it only shows up on the "home" page.

如果访客没有要求一个特别的文章,网页,类别,或者日期,函数is_home()只会产生一个真值,因此它只会显示在"主页" 上。

For more information, see WordPress:Creating a Static Front Page.

更多的信息,请看创建一个静态首页

Excerpts Only

只有摘录

The easiest way to display excerpts, instead of the full content, of posts, replace all instances of the_content() with the_excerpt(). If you have not created explicit excerpts for your posts, this function will automatically display the first 120 words of the post.

显示摘录而不是文章所有内容的最简单的方法是,用the_摘录()来取代所有的the_内容()的例子。如果你没有为你的文章创建摘录,这个函数会自动地显示文章的前120个词。

<div class="entry">
<?php the_excerpt(); ?>
</div>


<div class="entry">
<?php the_excerpt(); ?>
</div>


Showing Excerpts or Full Post Depending Upon Number of Posts

根据文章的数目来显示文章摘录或者全部的文章内容

In some circumstances, for example on archive pages, you may want to show the full post if there is only one post or excerpts if there are multiple posts. You can customize the loop to do this.

在有的情况下,例如,在文章档案网页中,如果只有一篇文章,你可能想要显示全部的文章内容,如果有几篇文章,你可能只想显示摘录部分。你可以自定义loop来显示全部内容或者摘录。

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

  <?php if (($wp_query->post_count) > 1) : ?>
     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for excerpts-->
          <?php the_excerpt() ?>
       <!-- Do your post footer stuff here for excerpts-->
     <?php endwhile; ?>

  <?php else : ?>

     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for single post-->
          <?php the_content() ?>
       <!-- Do your post footer stuff here for single post-->
     <?php endwhile; ?>

  <?php endif; ?>

<?php else : ?>
     <!-- Stuff to do if there are no posts-->

<?php endif; ?>




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

  <?php if (($wp_query->post_count) > 1) : ?>
     <?php while (have_posts()) : the_post(); ?>
       <!—你在这儿为摘录粘贴页眉吗-->
          <?php the_excerpt() ?>
       <!—你在这儿为摘录粘贴页脚吗-->
     <?php endwhile; ?>

  <?php else : ?>

     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for single post-->
          <?php the_content() ?>
       <!-- Do your post footer stuff here for single post-->
     <?php endwhile; ?>

  <?php endif; ?>

<?php else : ?>
     <!-- Stuff to do if there are no posts-->

<?php endif; ?>

Different Headers/Sidebars/Footers

不同的页眉/工具条/页脚

WordPress offers the get_header(), get_sidebar(), and get_footer() WordPress:Include Tags for use in your template files. These functions make it easy to define a standard header/sidebar/footer which is easily editable. Any changes made to these files will immediately be made visible to viewers, without any work on your part.

WordPress在你的模板文件中提供使用get_header(), get_sidebar(), 和 get_footer() 包括标签。这些函数使得定义一个标准的页眉/工具条/页脚变得简单,一个标准的页眉/工具条/页脚编辑简单。访客能够立马地看到这些文件中发生的任何变化,不包括你所执行的内容。

But sometimes you might not want a sidebar. If you don't want a sidebar, simply exclude the call to the get_sidebar() function from your template. For example, the single.php template in the WordPress default theme does not include a sidebar.

但是有时候,你可能不想要/em>一个工具条。如果你不想要一个工具条,只要移除调用到你的模板的get_sidebar()函数。例如,WordPress默认主题的single.php模板不包括一个工具条。

To create your own different sidebar, you have two choices.

要创建你自己的不同的工具条,你拥有两个选择。

  1. Include the sidebar contents directly into the template file on which you're working. If you want category-3 to have a different sidebar, edit category-3.php and include the necessary HTML and PHP to generate your distinctive sidebar.
  1. 将工具条内容直接包含到你正在处理的模板文件中。如果你想要类别-3拥有一个不同的工具条,编辑类别-3.php并且包含必须的HTML和PHP来产生你的独特的工具条。
  1. Use the PHP include function, to include another file. The WordPress get_sidebar() function only loads sidebar.php. If you make a file named sideleft.php, you would include it like this:
  1. 使用PHP包括函数来包括另一个文件。WordPressget_sidebar() 函数only 载入sidebar.php。如果你制作了一个文件,命名为sideleft.php,你可以像这样将它包含:
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>


<?php include(TEMPLATEPATH . '/sideleft.php'); ?>


Using the WordPress default WordPress:Template Hierarchy, if you want to use the same elements on multiple or different templates, it's probably best to put them in separate template files and use the PHP include() function. If the element you're adding is specifically for one template file, it's probably best to include it directly in that template file.

使用WordPress默认的模板层级,如果你想在多个或者不同的模板中使用相同的元素,最好将这些元素放进分开的模板文件中并且使用PHP include()函数。如果你添加的元素是专门为一个模板文件准备的,那么你最好直接将这个元素包含在那个模板文件中。

Summary

总结

We've just scratched the surface of what can be done with the Loop. As a reminder, the following are resources that will help you customize your own WordPress Loop.

我们刚刚初步了解了Loop的用法。作为一个提醒,下面的资源会帮助你自定义你自己的WordPress Loop

WordPress:Category:WordPress Lessons WordPress:Category:Design and Layout WordPress:Category:Advanced Topics




类别:WordPress课程 类别:设计和布局 类别:高级主题

Resources

资源