[wp-hackers] New proposed function: in_loop() to determine "in the loop" status

Mark Jaquith mark.wordpress at txfx.net
Sat Jul 16 22:36:40 GMT 2005

Until now, there's been no good way to figure out if you are currently 
in "the loop."  There were hacky methods, such as checking $post or 
$wp_query->current_post against $wp_query->post_count, but those methods 
either fail after the loop, or on the last post in the loop.

I've created a ticket with a set of patches that will fix this.

in_loop() returns true or false, depending on your in-the-loop status.

A new var for the WP_Query class, $in_loop, stores the status.  On each 
the_post() call, which calls $wp_query->the_post, the $in_loop var is 
set as true, as you are beginning a loop iteration.

The loop is ended when have_posts() (again, $wp_query->have_posts()) 
returns false.  I modified $wp_query->have_posts() to be aware that it 
has reached the last post, and to do the following:
1) set $in_loop false (that's the end of the loop)
2) run rewind_posts(), so that it is really easy to run through the loop 
3) unset the global $post object.  Normally this is left filled to the 
end of the page, and that's bloody useless.  The loop is over, this 
transient object needs to leave.

This also partially addresses this ticket:

How's this sound?

Mark Jaquith
MCincubus @ #wordpress

More information about the wp-hackers mailing list