WordPress:The Loop in Action

来自站长百科
Xxf3325讨论 | 贡献2008年4月22日 (二) 12:05的版本
跳转至: 导航、​ 搜索

介绍

"The Loop"是指WordPress主程序中的一个术语。你可以在你的模板文件中使用Loop来想访问者显示你的文章。你可以让模板不带有Loop,但这样你就只能显示来自一篇文章的数据了。

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

如果使用者不要求特定的文章,分类,页面或者日期,WordPress使用以前收集的默认值确定哪个文章准备给使用者阅读。例如,如果blog管理员在Administration > Settings > Reading设置中选择每页显示5篇文章,那么WordPress就会从数据库中取最新的5篇。如果使用者要求特定的文章,分类,页面或者日期,那么WordPress就会根据这些信息来选择从数据库中取出哪篇文章

一旦所有这些完成之后,WordPress就连接数据库,得到需要的信息,然后在一个变量中储存这个结果。进入这个变量的就是Loop,然后在模板中显示变量的值。 默认情况下,如果访问者没有选择特定的文章,页面,分类或者日期,WordPress会使用index.php显示所有内容。在这个Loop的讨论的第一部分,我们只集中讨论index.php,和默认的blog显示。接下来,一旦你懂得这些如何工作的时候,我们来研究Loop在别的模板文件中的作用。

世界上最简单的索引页面

接下来的是具有所有功能的索引,可以显示每个文章的内容(仅仅是内容),根据使用情况准备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(); 
?>

默认 Loop

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

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 .

生成文章

the_post()函数把文章集合中的现用的项目拿出来,并且让它可以在Loop的循环中使用。如果没有the_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>

文章内容

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

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

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

当查看某个单一的文章的时候,<!-- more -->分隔符会被忽略。这样如果读者想阅读全部内容,而又在所有的文章中都加如<!-- more -->分隔符的话,会迫使读者点击每个单独的文章。

详细资料附

在每个文章内容下边,在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>

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

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

自动寻找Trackback

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

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

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

结束 Loop

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

<?php endwhile; ?>

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

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

导航控制是不包含在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模板包括了边栏,最后是页脚。

其它模板中的Loop

WordPress可以使用不同的模板文件,用不同的方式显示你的blog。在默认WordPress主题中,有用于索引浏览的模板文件,分类浏览和文档浏览,就象一个浏览单独文章的模板。这些都会用到Loop,但是这些都没有太大的差距,就象模板标签之间的不同用法似的。

至于那些没有分开的模板文件的情况,WordPress使用默认时的index.php。如果访问者请求阅读一个单独的文章时,WordPress会首先查找一个名字为single.php的文件。如果这个文件存在,它就会用来显示这个文章。如果不存在,WordPress就会使用index.php来显示文章。这叫做模板层次.

如果你正在制作你自己的主题,在默认主题中查看模板文件,作为参考,很有用。同样,使用你的主题的index.php作为你的其他模板文件的模板也是很有用的。因为你创建了更多的模板文件,这样做可能会带给你一个已知的工作页面,从这里开始作出更改。

不同的归档格式

archive是历史文章的集合。在默认时,文章在主索引中显示的是最新的按时间顺序的记录。当访问者点击某个文档连接时,或者他们手动请求某个特定时间时,(使用http://www.example.com/blog/index.php?m=200504或者 http://www.example.com/blog/2005/04 来选择所有2005年4月后的文章),WordPress将显示archive内容。默认情况下,归档将使用index.php,然后和你的首页一样,只显示出2005年四月后的文章。

当WordPress为访问者准备归档界面时,它会在你现用的主题目录中明确的寻找一个叫做archive.php的文件,如果你想在首页上使归档的意思明确表达,那么把index.php复制到archive.php,并且按需求编辑archive.php文件。

例如,如果你只想在归档列表上显示文章标题,不包含文章内容,你可以使用如下代码:

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

不同的分类格式

和归档界面一样,WordPress为分类界面寻找分开的模板文件。如果访问者点击了一个分类连接,他们会看到分类外观,WordPress会只从分类中准备带有文章的Loop,限制每个blog默认设置下的文章的数目。

要想让你的分类界面和索引界面不同的话,复制一个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(); ?>

不同分类的不同格式

如在模板层次中叙述的,为每个分类创建分开的模板文件是可行的。只需要建立名字为category-X.php的文件,这里X是用数字表示的分类。仔细考虑你是否需要给某个分类建立全新的完整模板。

让我们来看两个分类,"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 ?>

如果你添加了另外一个分类,"Cars",你想让它用一个引人注目的方式显示出来,那么一个分开的category-X.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文件联系在一起的了.

不同的单一文章格式

当浏览任意单一的文章(或者 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>

这种信息—不论评论是公开还是关闭—是相当不适合放在一个索引上,归档或者分类界面的;那也是为什么只包含在single.php模板文件中的原因。

其他Loop技巧

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

静态首页

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

想让这个Loop技巧实现,使用is_home()条件式模板标签函数。

在你的 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
} ?> 

如果访问者请求某个具体文章,页面,分类或者日期时,函数is_home()不会产生真值,这样它只显示在"主"页上。

参见创建一个静态首页以获得更多信息。

只显示摘录

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

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

显示摘录或者全部文章取决于文章编号

某些情况下,如在文档页面上,如果只有一篇文章的话你可能想显示全部的内容,或者有多篇文章显示摘录。你可以自定义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

资源