Created
June 25, 2013 15:13
-
-
Save johnpbloch/5859272 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Loopable_Query extends WP_Query implements Iterator { | |
/** | |
* (PHP 5 >= 5.0.0)<br/> | |
* Return the current element | |
* @link http://php.net/manual/en/iterator.current.php | |
* @return mixed Can return any type. | |
*/ | |
public function current() { | |
if($this->current_post === -1){ | |
$this->the_post(); | |
} | |
return $this->post; | |
} | |
/** | |
* (PHP 5 >= 5.0.0)<br/> | |
* Move forward to next element | |
* @link http://php.net/manual/en/iterator.next.php | |
* @return void Any returned value is ignored. | |
*/ | |
public function next() { | |
$this->the_post(); | |
} | |
/** | |
* (PHP 5 >= 5.0.0)<br/> | |
* Return the key of the current element | |
* @link http://php.net/manual/en/iterator.key.php | |
* @return mixed scalar on success, or null on failure. | |
*/ | |
public function key() { | |
return $this->current_post >= 0 ? $this->current_post : 0; | |
} | |
/** | |
* (PHP 5 >= 5.0.0)<br/> | |
* Checks if current position is valid | |
* @link http://php.net/manual/en/iterator.valid.php | |
* @return boolean The return value will be casted to boolean and then evaluated. | |
* Returns true on success or false on failure. | |
*/ | |
public function valid() { | |
return $this->have_posts(); | |
} | |
/** | |
* (PHP 5 >= 5.0.0)<br/> | |
* Rewind the Iterator to the first element | |
* @link http://php.net/manual/en/iterator.rewind.php | |
* @return void Any returned value is ignored. | |
*/ | |
public function rewind() { | |
$this->rewind_posts(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment