WordPress:The Loop in Action

来自站长百科
Seadragon530讨论 | 贡献2008年4月18日 (五) 16:53的版本
跳转至: 导航、​ 搜索

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所做的第一件事就是检查它所需要的所有文件是否存在。接下来,它按照blog 管理员定义的,从数据库收集默认的设置。这包括每页显示的文章的数量,是否允许评论,等等。一旦这些默认的内容确定后,WordPress就会检查使用者要求什么。这个信息用来确定从数据库中取出哪篇文章。s

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使用以前收集的默认值确定哪个文章准备给使用者阅读。例如,如果blog管理员在Administration > Settings > Reading设置中选择每页显示5篇文章,那么WordPress就会从数据库中取最新的5篇。如果使用者要求特定的文章,分类,页面或者日期,那么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,和默认的blog显示。接下来,一旦你懂得这些如何工作的时候,我们来研究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.

<?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,给你看这些的唯一目的就是表明这些对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(); 
?>

The Default 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

接下来是一步一步的来看WordPress v1.5标准安装,默认情况下,默认经典主题中loop用法。

Begin The Loop

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

<?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.
  2. 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.
  3. 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.

Loop开始

在默认index.php模板文件的顶部,是Loop代码开始的地方。

<?php if (have_posts()) : ?><br />
<?php while (have_posts()) : the_post(); ?>
  1. 首先它用have_posts()功能检查是否有新的文章.
  2. 如果有文章的话,一个PHP while的loop就开始了。只要插入语为逻辑真,while loop就会继续执行。这样只要函数have_posts() 返回真值,Loop就会继续。
  3. 函数have_posts()简单的在文章集合中检查下一个项目:如果有另外一个项目,返回true,如果没有下一个项目,返回false .

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:

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

文章内容

the_content()模板标签显示文章的内容。 这是每个通过Loop的很重要的部分:

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

如果你把more这个按钮包含到Quicktag,然后象这样<!--more-->显示出来, 在你的文章的正文部分,只有above这部分才会显示给访问者。这样,如果你只是想让你的首页面显示每篇文章的第一句或者前两句话的话,只需要简单的在每篇文章的第一行之后插入 <!--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.

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

详细资料附

在每个文章内容下边,在index.php模板文件中,是显示有关这个文章更多信息的地方,如分类,日期和评论信息。大家知道的文章meta数据部分,如果你是一个已经登陆的有充分权限的使用者,你可会看见"Edit This"连接,这多亏了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>


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.

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.

如果评论被激活,或者是该文章有评论内容,comments_popup_link()模板标签将回显示一个到评论内容的连接。如果你使用了评论弹出窗口,这个连接就会打开评论窗口,否则它就会直接跳转到这篇文章的评论内容。

如果访问者在浏览文章索引(i.e.: 在Loop中不止一篇文章),comments_popup_link()连接将会把读者连接到这篇文章的单独页面。

Trackback Autodiscovery

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

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

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

自动寻找Trackback

trackback_rdf模板标签的功能是输出机读代码用于自动寻找trackback

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

注意:trackback_rdf()标签支持在自身旁边使用评论。它并非"关闭"的。

Ending The 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.

<?php endwhile; ?>

结束 Loop

下面是结束 Loop。在这之后,各种文章相关的模板标签不再如如你想要的那样工作了(如果它们在工作,它们会使用Loop的最后一篇文章).这意味着,如果你需要使用一个工作在 Loop内的模板标签,你需要把如下语句放进去。

<?php endwhile; ?>

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

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

在这个部分,在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>

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.

如果blog设置成每页显示10篇文章,而且Loop收集到了25篇文章,这就会产生三个页面:两个10篇文章的页面,还有一个五篇的页面。导航连接允许访问者通过文章收集跳转到上一还是下一页。

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.

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

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

导航控制是不包含在Loop内的,但是包含在if 条件句内,这样它们只能显示是否有文章。导航函数本身也会检查是否有一些基于现有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>

else :语句决定了如果have_posts()返回false时做些什么。那就是说,else之后的部分只能在Loop没有文章时才会被显示。没有文章显示出来,举例来说,访问者要求某个特殊日子的内容,但是那天没有文章,或者是搜索但是没有结果。

  <?php endif; ?>

这个语句结束了这样的条件句:"如果有文章这样,如果没有文章那样".一旦条件语句结束了,默认的index.php模板包括了边栏,最后是页脚。

The Loop In Other Templates

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.

其它模板中的Loop

WordPress可以使用不同的模板文件,用不同的方式显示你的blog。在默认WordPress主题中,有用于索引浏览的模板文件,分类浏览和文档浏览,就象一个浏览单独文章的模板。这些都会用到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.

不同的文档格式

archive是历史文章的集合。在默认时,文章在主索引中显示的是最新的按时间顺序的记录。当访问者点击某个文档连接时,或者他们手动请求某个特定时间时,(使用http://www.example.com/blog/index.php?m=200504或者 http://www.example.com/blog/2005/04 来选择所有2005年4月后的文章),WordPress将显示archive内容。默认情况下,文档将使用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为访问者准备文档界面时,它会在你现用的主题目录中明确的寻找一个叫做archive.php的文件,如果你想在首页上使文档的意思明确表达,那么把index.php复制到archive.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(); ?>

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,限制每个blog默认设置下的文章的数目。

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

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是用数字表示的分类。仔细考虑你是否需要给某个分类建立全新的完整模板。

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:

  • Use two separate files, category-3.php and category-4.php, each with a different img tag for each post title.
  • 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:
<?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 ?>

让我们来看两个分类,"Plants" 和 "Flowers",分类ID分别为3和4。在输出的地方的每个文章标题旁边,你都想要有个植物或者花的图片,取决于哪个分类被显示,你可以这样:

  • 使用两个分开的文件, category-3.phpcategory-4.php, 每个文件给每个文章标题使用不同的img标签。
  • 在你的默认category.php文件中使用条件判断,来查看是否当前分类是"Plants"或者 "Flowers" (或者都不是), 然后显示合适的图片:
<?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 ?>

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.

如果你添加了另外一个分类,"Cars",你想让它用一个引人注目的方式显示出来,那么一个分开的category-X.php则是你最合适的选择。

Different CSS For Different Categories

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:

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

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.

不同分类的不同CSS

很多使用者想给特定的分类建立单独的CSS文件。这很容易实现。记住样式表是在HTML文件中的<head>部分定义并且加载的。WordPress使用header.php文件,在默认header.php文件中,找到下列语句:

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

注意: 汽车分类模板使用category-5.css文件取代默认版面。在这个例子中,CSS文件在应用的分类模板文件之后被命名,而不是真正的分类名。这样你就明白 category-5.css 是和category-5.php文件联系在一起的了.

Different Single Post Format

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

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

不同的单一文章格式

当浏览任意单一的文章(或者 permalink)的时候,WordPress会使用single.php。如果有的话。

在这个部分,在WordPress默认的single.php文件中,提供了有关当前文章的文章meta数据信息

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

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

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.

其他Loop技巧

现在你已经对WordPress Loop的基本使用有了一个全面的了解,我们开始介绍更多的Loop效果和技巧。

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.

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

静态首页

你如何显示一些blog中特别的仅仅存在于你的首页上的东西呢?对,就是只在你的首页或者主页上的,站点上别的任何地方都看不到的。简单!我们把这称为静态首页。站点的首页并不真的是静态的。它只是使用了Loop让它看起来如此罢了。

想让这个Loop技巧实现,使用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
} ?> 

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.

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

只显示摘录

显示摘录而不是全文的最简单的方法,把所有的the_content()实例用the_excerpt()代替,如果你没有建立你的文章的外部摘录,这个函数将自动的显示文章的前120个词。

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

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.

有时你可能不要边栏。如果你不想要边栏,只需简单的把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.
  2. 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:
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>

眼建立你自己的不同的 边栏,你有两种选择。

  1. 把边栏内容直接包含到你正在操作的模板文件中。如果你想要category-3 拥有不同的边栏, 编辑category-3.php,把必须的HTML和PHP代码包含进去来生成这个唯一的边栏。
  2. 使用PHP的包含 函数, 来包含另外一个文件。 WordPress get_sidebar() 函数 加载sidebar.php. 如果你有个文件名为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默认的模板层次,如果你想在多个或者不同的模板中使用相同的元素,你最好把它们放到分开的模板文件中,并使用PHPinclude()函数。如果这个你添加的元素是特别为某一模板使用的,最好是把它直接包含到那个模板中去。

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.

概要

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

Resources

资源