Make WordPress Core

Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#1518 closed defect (bug) (fixed)

Determine if you are currently in "the loop" [PATCH]

Reported by: markjaquith Owned by: markjaquith
Milestone: Priority: normal
Severity: normal Version: 1.6
Component: Administration Keywords: bg|dev-feedback bg|2nd-opinion bg|has-patch
Focuses: Cc:

Description (last modified by markjaquith)

A lot of functions need to behave differently inside the loop as opposed to outside of the loop. These patches create the function in_loop() which returns true or false, based on whether or not you are currently in the loop.

A new var for $wp_query has been created: $in_loop

With each $this->the_post() call, $this->in_loop is set as true.

When the loop is over, and $this->have_posts() would normally just return false, $this->in_loop is set as false, the posts are rewinded, and the global $post variable is set $post = "";

This addresses part of #977

This function could also be used internally to protect functions that must be used inside the loop from being used outside of it (or at least providing an elegant error message so people know what the problem is).

Patch is against latest 1.6 SVN

Attachments (2)

classes.php.diff (949 bytes) - added by markjaquith 10 years ago.
1.6 SVN patch
functions.php.diff (356 bytes) - added by markjaquith 10 years ago.
1.6 SVN patch

Download all attachments as: .zip

Change History (6)

comment:1 @markjaquith10 years ago

  • Description modified (diff)
  • Status changed from new to assigned

comment:2 @markjaquith10 years ago

  • Description modified (diff)

@markjaquith10 years ago

1.6 SVN patch

@markjaquith10 years ago

1.6 SVN patch

comment:3 @ryan10 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [2716]) in_the_loop() Props: Mark Jaquith fixes #1518

comment:4 @anonymous8 years ago

  • Milestone 2.0 deleted

Milestone 2.0 deleted

Note: See TracTickets for help on using tickets.