Wordpress 中文
注册
Advertisement

介绍[ | ]

"The Loop" 是一个同 WordPress 主进程(main process)相关的东西。你在template files中使用Loop来显示帖子。你也可以构建没有Loop的模板,那样的话你就只能显示一个帖子。

WordPress 首先检查所有需要显示的文件。接下来它从数据库中收集你在blog administrator中的缺省设置信息。比如说每页显示的帖子数量,是否开放评论功能等等。一旦这些信息明确了,WordPress接下来响应用户的请求。从而决定从数据库中提取哪个帖子。

如果用户没有请求任何特定的帖子,分类,页面,或数据,WordPress使用之前收集到的缺省值来决定为用户准备那个帖子。比方说,如果blog管理员选择display 5 posts per page,WordPress将从数据库中提取五个最近发布的帖子。如果用户明确指定了某个帖子,分类,页面或数据,WordPress将据此从数据库中提取帖子。

一切就绪后,WordPress连接数据库,提取信息存到变量中。Loop将使用这个变量,然后在你的模板中显示出来。

如果访问者没有选定某个特定的帖子,页面,分类,或数据,WordPress缺省使用index.php来显示所有东西。作为Loop讨论的第一部分,我们首先关注index.php,缺省的blog显示页。稍后,一旦你理解了运作原理,我们将探讨在其他模板文件中的应用。

世界第一超级简单 Index 页面[ | ]

下面是一个全功能的 index 页,他会依据Loop的设定条件显示每个帖子的内容(只显示内容。这个代码就是向你显示 Loop 实现起来有多简单。其实index.php中的绝大多数代码都是用来美化页面的CSS, HTML, PHP 。

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

现在,就让我们看看绝大多数用来美化Loop的东东。

缺省 Loop[ | ]

下面我们一步步来学习Wordress v1.5 标准安装中的defaultclassic的缺省的Loop用法。

开始 Loop[ | ]

在缺省模板文件中的 index.php 顶部就是开始The Loop的代码。

<?php if (have_posts()) : ?><br />
<?php while (have_posts()) : the_post(); ?>
  1. 首先它通过have_posts()函数来检查是否有帖子。
  2. 如果有帖子,开始一个PHP的while循环。一个while loop 将检索出所有符合()中传入条件的记录。只要have_posts()返回为true,loop将不断循环。
  3. 函数have_posts()只是简单的检索下一个帖子:如果存在,返回true;否则,返回false.

生成 Post[ | ]

the_post() 函数在Loop中获取当前的帖子并使其有效。如果没有the_post(),,你的theme中的很多Template Tags将无法工作。 一旦某个帖子被置为有效,模板就会向访问者显示该帖子中的数据。

标题,日期和作者[ | ]

下面的template tags获取当前帖子的标题,张贴日期和发帖人。

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

帖子正文(content)内容[ | ]

the_content() template tag 会显示帖子正文内容。这是一个Loop循环要作的基本工作。

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

如果你包含一个称之为moreQuicktag按钮,你的帖子会在正文中显示一个<!--more-->它会隐藏标记后的正文内容。比如你只是想显示每个帖子内容的头一两句,简单的将<!--more--> 标记插入到你的每个帖子的头一行后面。 当浏览一个帖子的时候<!-- more -->之后的内容会隐藏。如果你想强制浏览者通过点击进入来阅读全文,那么就在你的帖子中都加上这个标记吧。

其它细节问题[ | ]

index.php模板文件中帖子正文之下,是用来显示帖子信息的地方,比如分类,日期,以及评论。正如post meta data section所述,如果你有登录进去后有足够权限或者本身为帖子的作者,你还会看到一个"Edit This" 的连接,这要归功于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>

如果评论功能被打开,或者帖子已经有了评论,comments_popup_link() 模板标记(template tag)会显示一个评论链接。如果你使用comments popup window,点击这个链接会打开一个评论窗口;否则它会跳转到这个帖子的评论内容。

如果访问者正在浏览帖子的索引(注意:指Loop循环中有多个帖子的情况), comments_popup_link()链接会把读者指引到这个帖子的单独的页面。

Trackback 自动搜索[ | ]

trackback_rdf 模板标记函数(template tag's function)用来输出机器可以识别的代码,这个代码可以用来实现trackback的自动搜索。

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

备注:trackback_rdf() 标记(tag)预定为被comments封装。这个功能无法 "关闭"。

结束 Loop[ | ]

下面的代码结束Loop。这些工作完成后,各种与帖子相关的模板标记还不会发挥预期的作用,(or if they do, they will use the last post from The Loop)。这也就是说,如果你希望在循环内部让模板标记(template tag)生效,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.

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

假设每页显示10个帖子,Loop查询出25个帖子,那导航条会有3个页面:其中两个有10个帖子,一个有5个帖子。访问者可以通过导航条链接前后翻阅所有这些帖子。

导航条控制被包含在Loop以外,但是条件判断在Loop以内,(又卡壳了,下面保留原文)

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>

else :子句用来处理最上面的have_posts()返回为false的情况。就是说else后的代码仅仅用来处理没有帖子时的情况。只有当访问者试图访问的某一天没有帖子,或者搜索时没有符合条件的帖子时,这种情况才会发生。

  <?php endif; ?>

这里结束这个条件判断:“如果有帖子,do this,否则没有,do that”。条件判断结束后,缺省的 index.php 模板接下来会包含sidebar,以及最后的 footer。

其它模板文件中的 Loop[ | ]

WordPress允许使用不同的模板文件通过各种不同的方法来显示 blog。在WordPress缺省主题中,有不同的模板文件来分别对应 索引浏览,分类浏览,归档浏览,就像浏览单独的帖子一样。这每一个文件都会用到The Loop,但格式稍微不同,就行template tags的用法也有所不同一样.

对于没有单独的模板文件的,WordPress会缺省使用index.php。如果一个访问者浏览单独一个帖子,WordPress会首先寻找single.php文件。文件如果存在,就通过它来展示帖子。如果不存在,就使用index.php来展示。这被称为Template Hierarchy。(模板继承?这个词C++里常见,记不清是重载还是继承了。)

如果你在设计自己的Theme, 学习一下缺省主题的模板文件会是个很好的起点。使用你自己主题的index.php作为其它模板的样本也是个不错的主意。至少这样会给你一个已知可以正常运转的页面,在此基础上你可以不断修修补补。

不同的归档(archive)格式[ | ]

归档就是一个所有历史帖子的大杂烩。缺省情况下,是按照时间把最近的帖子显示在首页上。当访问者点击'归档'链接时,或者手工选定了一个特定日期(http://www.example.com/blog/index.php?m=200504 或 http://www.example.com/blog/2005/04 用于选定2005年4月份的所有帖子), WordPress 会显示一个 归档 视图。缺省情况下,归档使用index.php,以此它看起来跟你的页面一样,只是显示的是2005年4月的帖子。

WordPress展示归档 视图时,他会寻找你的主题目录下的archive.php模板文件。如果你希望你的归档看起来和主页面风格一致,只需简单的把index.php copy成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(); ?>

不同的分类(category)格式[ | ]

就像归档视图一样,WordPress也会寻找单独的分类视图模板文件。如果访问者点击某个分类链接,他就会看到分类视图。WordPress会根据blog每页显示帖子数量的缺省设置,为这个分类下的所有帖子构建一个Loop。

要使你的分类视图同index视图有所不同,拷贝index.phpcategory.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(); ?>

为不同分类设置不同格式[ | ]

正如前面对Template Hierarchy 模板继承的描述,为每个分类创建单独的模板文件是完全可能的。所有操作无非是命名一个category-X.php文件,其中X表示分类项目的ID号。请仔细考虑你是否需要为一个指定分类创建一个全新的模板。

让我们假设这样两个分类"Plants" 和"Flowers",ID号分别为3和4。你在点击浏览下一个帖子时,可能希望在plant或flower分类下的每个帖子标题上都会带上一幅图片。你可以这样作:

  • 使用两个不同文件,category-3.phpcategory-4.php,每个分类下的帖子标题需要各自的img 标记。
  • category.php 文件中构建一个条件测试,判断如果当前分类为"Plants" 或 "Flowers" 时,显示对应的图片:
<?php if (in_category('3') ):
 // we're in the Plants category, so show a plant ?>
 <img src='/images/plant.png' alt='a plant' />
<?php } elseif (in_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文件。这同样是很容易实现的。重要的一点是要记住样式表(stylesheets)是在一个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 ( in_category('5') ) { // Load special CSS for "Cars" category ?>
  <link rel="stylesheet" href="<?php get_template_directory(); ?>/category-5.css" type="text/css" media="screen" />
<?php } else { ?>
   <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<?php } ?>

提示: Cars 分类的模板使用 category-5.css 文件来重载(override)页面布局。在这个例子中,CSS 文件同分类模板文件的关联已经确定,因此被明确命名,你的实际分类命名编号可能不同。不管怎样,你只要知道 category-5.csscategory-5.php匹配就行了。

单一帖子(single post)的不同格式[ | ]

当浏单一帖子(single post)(或permalink)时,WordPress 会使用single.php模板,如果存在的话。 这一部分,来自WordPress的缺省 single.php模板,针对当前展现帖子提供post meta data information

<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技巧和功能。

静态页面(Front Page)[ | ]

你怎么展示你的blog上只想放到 front page上的东西?问得好,对于只想放到 front page 或 home page 上,以及站点上任何其他地方不适合展示的东西,我们称之为静态页面。你的站点上的front page 或者说首页并未是静态的。它只是 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()函数就会返回true,它用来控制显示在"home"页面上的东西。

要了解更多信息,参考创建静态 Front Page.

只显示摘录信息[ | ]

如果不想显示帖子全文,而只是显示摘录信息,那有一个更简单的方法,用the_excerpt()替换所有the_content()实例。如果你没有为帖子创建摘要信息,这个函数会自动选取显示帖子的头120个单词。

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

根据帖子数量决定显示全文还是摘要[ | ]

In some curcumstances, 举例来说在归档(archive)页面上, 当只有一个帖子时,你会考虑显示全文,而有多个帖子时,你会考虑显示摘要。你可以定制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; ?>

定义不同的 Headers/Sidebars/Footers[ | ]

WordPress提供了get_header(), get_sidebar(), and get_footer() 包括 Tags供你的模板文件调用。这些函数可以简化标准的 header/sidebar/footer 的定义和编写。任何对这些模板文件的修改会立即影响到访问者看到的页面外观,因此你并不是在暗箱操作。

但有的时候你并不需要一个sidebar。这时只需要简单的将 get_sidebar() 函数从你的模板文件中去除就可以。比如缺省主题下的 single.php 模板文件就没有包含 sidebar。

要定制个性化的 sidebar,有两种办法。

  1. 将sidebar的所有内容直接写入你希望它显示的地方所相关的模板文件。如果你想让 category-3有一个不同的 sidebar,编辑category-3.php文件,加入一些必要的HTML和PHP来定制你的sidebar。
  1. 使用PHP include 函数将其他文件包括进来。WordPress 的get_sidebar() 函数只是 载入sidebar.php文件。如果你自己写了个文件sideleft.php,你可以这样把它包括进来:
<?php include(TEMPLATEPATH . '/sideleft.php'); ?>

基于WordPress的模板继承Template Hierarchy,如果你想在多个或者不同的模板中使用相同的页面元素,在各个模板文件中使用 PHP的include()函数将将他们包括进来,这是最佳的处理办法。如果添加的元素只是针对个某个模板文件的,那么直接将他添加到那个文件中。

总结[ | ]

此文只是简单的概述了一下Loop及其功能。提示一下,要想构建你自己的WordPress Loop,下列资源会十分有用。

资源[ | ]

Advertisement