rewind_posts()
Rewinds the loop to the beginning.
Used to restart the WordPress Loop (the loop).
No Hooks.
Returns
null. Returns nothing.
Usage
<?php rewind_posts(); ?>
Examples
#1 Using one cycle 2 times
Example of how to apply rewind_posts() to use the same loop 2 times per page:
<?php
// Use the loop for the first time
if( have_posts() ){
while( have_posts()){
the_post();
?>
<!--display posts -->
<h2><?php the_title(); ?></h2>
<?php
}
}
?>
<!--any code -->
<?php
// Use the loop a second time
// rewind the loop to the beginning to use `have_posts()` again
rewind_posts();
if( have_posts() ){
while( have_posts() ){
the_post();
?>
<!--display posts -->
<h2><?php the_title(); ?></h2>
<?php
}
}
?> #2 Example with custom query
<?php $args = array( 'posts_per_page' => -1 ); $my_posts = new WP_Query($args); if ($my_posts->have_posts()) : while ($my_posts->have_posts()) : $my_posts->the_post(); ?> <?php the_content(); ?> <?php endwhile; endif; ?> // rewind <?php $my_posts->rewind_posts(); ?> // new loop <?php while ($my_posts->have_posts()) : $my_posts->the_post(); ?> <?php the_content(); ?> <?php endwhile; ?>
Notes
- Global. WP_Query.
$wp_queryWordPress Query object.
Changelog
| Since 1.5.0 | Introduced. |
rewind_posts() rewind posts code WP 7.0
function rewind_posts() {
global $wp_query;
if ( ! isset( $wp_query ) ) {
return;
}
$wp_query->rewind_posts();
}