在wordpress中读取文章列表都是通过loop形式获取的,”Loop(循环)”是一个指明WordPress主要程序过程的术语。你在你的模板template files中应用循环来把你的文章表现给读者。特别的在我们制作wordpress主题首页过程中,经常需要遍历数据,读取最新的文章列表,接下来让我们具体来看下wordpress Loop(循环)的使用,以下我们以主题首页模板index.php为例:
<?php
get_header();
//Loop 开始
if (have_posts()) :
while (have_posts()) :
the_post();
the_content();
endwhile;
endif;
//Loop结束
get_sidebar();
get_footer();
?>
以上实例仅展示了每篇文章的内容,使用中视具体情况去调整循环
在以上index.php实例中,可以看到Loop如何开始的代码为:
<?php if (have_posts()) : ?> <?php while (have_posts()) : 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()函数直接输出获取。get_the_content()为返回文章内容,你可以对读取的文章内容进行过滤截取。
<div class="entry">
<?php the_content('阅读全文 »'); ?>
</div>
更多wordpress Loop请查看:http://codex.wordpress.org/zh-cn:The_Loop_in_Action