描述:
判断是否有文章可以循环输出
have_posts函数被调用时,实际上是调用全局变量$wp_query->have_posts()成员函数,来简单检查一个全局数组(array)变量$posts的一个循环计数器,以确认是否还有post,如果有返回true(1),如果没有返回false(0)。
该函数常用于循环中。
用法:
<?php have_posts(); ?>
参数:
该函数不需要传递任何参数
示例:
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Your loop code
endwhile;
else :
echo wpautop( 'Sorry, no posts were found' );
endif;
?>
源文件:
/**
* Whether current WordPress query has results to loop over.
*
* @since 1.5.0
*
* @global WP_Query $wp_query
*
* @return bool
*/
function have_posts() {
global $wp_query;
return $wp_query->have_posts();
}