Codex tools: Log in
Languages: English • Italiano • (Add your language)
Contents |
This function checks to see if the current WordPress query has any results to loop over. This is a boolean function, meaning it returns either TRUE or FALSE.
At the end of the loop, will automatically call rewind_posts.
<?php have_posts(); ?>
This function does not accept any parameters.
The following example can be used to determine if any posts exist, and if they do, loop through them.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); // Your loop code endwhile; else : echo wpautop( 'Sorry, no posts were found' ); endif; ?>
Calling this function within the loop will cause an infinite loop. For example, see the following code:
<?php
while ( have_posts() ): the_post();
// Display post
if ( have_posts() ): // If this is the last post, the loop will start over
// Do something if this isn't the last post
endif;
endwhile;
?>
If you want to check if there are more posts in the current loop without this unfortunate side effect, you can use this function.
function more_posts() {
global $wp_query;
return $wp_query->current_post + 1 < $wp_query->post_count;
}
have_posts() is located in wp-includes/query.php.