原文链接:http://www.wordpress.la/codex-WordPress%E4%B8%BB%E5%BE%AA%E7%8E%AF.html
WordPress主循环
WordPress用主循环(The Loop)显示用户的文章。通过The Loop,WordPress可以将每篇文章显示在当前页面,并将这些文章按The Loop标签中的指定标准进行排版。The Loop中的任何HTML或PHP代码重复出现在每篇文章上。若WordPress文档宣布“该标签必须出现在The Loop中”,于是指定的模板标签或插件就会重复出现在每篇文章中。
例如,WordPress 1.5中The Loop默认显示的信息包括:每篇文章的标题((the_title()),时间(the_time())以及类别(the_category())。关于文章的其它信息则由相应的模板标签或(仅供高级用户)通过访问$post变量显示,运行The Loop时已经在当前文章信息中设置了该变量。
The Loop基本知识参见运行中的The Loop(主循环)。
使用The Loop
The Loop应该存储在index.php以及任何用以显示文章信息的模板中,存放位置取决于WordPress版本。所以请先“确定正在使用的WordPress版本”。
WordPress 1.5 - 2.7
在主题模板的顶端应该包含对页眉模板的调用。如果用户在自己的版块设计(设计的不是模板)中使用The Loop,需要将WP_USE_THEMES设为false。
<?php define('WP_USE_THEMES', false); get_header(); ?>The Loop开始于:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>结束于:
<?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>WordPress 1.2
<?php /* Don't remove this line. */ require('./wp-blog-header.php'); ?>The Loop开始于:
<?php if ( $posts ) : foreach ( $posts as $post ) : start_wp(); ?>结束于:
<?php endforeach; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?>
<!-- Start the Loop. --> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <!-- The following tests if the current post is in category 3. --> <!-- If it is, the div box is given the CSS class "post-cat-three". --> <!-- Otherwise, the div box will be given the CSS class "post". --> <?php if ( in_category('3') ) { ?> <div class="post-cat-three"> <?php } else { ?> <div class="post"> <?php } ?> <!-- Display the Title as a link to the Post's permalink. --> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <!-- Display the Time. --> <small><?php the_time('F jS, Y'); ?></small> <!-- Display the Post's Content in a div box. --> <div class="entry"> <?php the_content(); ?> </div> <!-- Display a comma separated list of the Post's Categories. --> <p class="postmetadata">Posted in <?php the_category(', '); ?></p> </div> <!-- closes the first div box --> <!-- Stop The Loop (but note the "else:" - see next line). --> <?php endwhile; else: ?> <!-- The very first "if" tested to see if there were any Posts to --> <!-- display. This "else" part tells what do if there weren't any. --> <p>Sorry, no posts matched your criteria.</p> <!-- REALLY stop The Loop. --> <?php endif; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <!-- If the post is in the category we want to exclude, we simply pass to the next post. --> <?php if (in_category('3')) continue; ?> <div class="post"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y'); ?></small> <div class="entry"> <?php the_content(); ?> </div> <p class="postmetadata">Posted in <?php the_category(', '); ?></p> </div> <!-- closes the first div box --> <?php endwhile; else: ?> <p>Sorry, no posts matched your criteria.</p> <?php endif; ?>注意:使用HTML代码时一定要在<?php ?>标签外使用。PHP代码(即使是大括号)都必须放在 <?php ?>标签内。即使在if和else语句中,用户也可以启动或停止PHP代码以点缀HTML代码。如上例所示。
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <!-- If the post is in the category we want to exclude, we simply pass to the next post. --> <?php if (in_category('3')) continue; ?> <div class="post"> <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y'); ?></small> <div class="entry"> <?php the_content(); ?> </div> <p class="postmetadata">Posted in <?php the_category(', '); ?></p> </div> <!-- closes the first div box --> <?php endwhile; else: ?> <p>Sorry, no posts matched your criteria.</p> <?php endif; ?>