Skip to content

Instantly share code, notes, and snippets.

@beneverard
Forked from tomjn/gist:6140909
Created August 12, 2013 12:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beneverard/6210413 to your computer and use it in GitHub Desktop.
Save beneverard/6210413 to your computer and use it in GitHub Desktop.
<?php
$pages = new query_loop( array(
'post_type' => 'page'
));
foreach( $pages as $id => $post ) {
the_title();
// etc...
}
// put this below in your functions.php:
/**
* WordPress Main Posts Loop Iterator
*/
class query_loop implements Iterator, Countable {
public function __construct( $args = array() ) {
$this->query = new WP_Query( $args );
}
function count() {
return count($this->query->posts);
}
function rewind() {
$this->query->rewind_posts();
}
function current() {
return $this->query->post;
}
function key() {
return $this->query->post->ID;
}
function next() {
$this->query->the_post();
}
function valid() {
if ( $this->query->have_posts() ) {
return true;
} else {
wp_reset_postdata();
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment