Skip to content

Instantly share code, notes, and snippets.

@johnpbloch
Created June 25, 2013 15:13
Show Gist options
  • Save johnpbloch/5859272 to your computer and use it in GitHub Desktop.
Save johnpbloch/5859272 to your computer and use it in GitHub Desktop.
<?php
class Loopable_Query extends WP_Query implements Iterator {
/**
* (PHP 5 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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 &gt;= 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