WordPress的have_posts()和the_post()函数

在WordPress的index.php文章循环输出中,通常会有下面一段代码:

[pcsh lang="php" tab_size="4" message="" hl_lines="" provider="manual"]


<?php if (have_posts()) : ?>
	<?php while (have_posts()) : the_post(); ?>
	<?php endwhile; ?>
<?php endif; ?>

[/pcsh]

这里有两个函数,have_posts()和the_post()。

have_posts() 函数:
WordPress的have_posts() 默认是一个全局函数。

have_posts函数被调用时实际上是调用全局变量$wp_query->have_posts()成员函数,来简单检查一个全局数组(array)变量$posts的一个循环计数器,以确认是否还有post,如果有返回true(1),如果没有返回false(0)。

the_post()函数:

the_post()函数则调用$wp_query->the_post()成员函数前移循环计数器,并且创建一个全局变量$post(不是$posts),把当前的post的所有信息都填进这个$post变量中,以备接下来使用。

简单的使用可以通过函数来直接执行,如

the_content()直接显式post的内容,

the_title()显式帖子的标题,

the_time()显示帖子的时间等WORDPRESS的Template Tags。

高级应用或要定制应用则可以直接调用$post变量的成员。

此处评论已关闭