Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active December 20, 2015 13:39
Show Gist options
  • Save tomjn/6140601 to your computer and use it in GitHub Desktop.
Save tomjn/6140601 to your computer and use it in GitHub Desktop.
If you're lucky enough to be using any version of PHP 5 with WordPress ( 100% chance! ) You can do this:
<?php
$posts = new posts_loop();
foreach( $posts as $id => $post ) {
the_title();
// etc...
}
// put this below in your functions.php:
/**
* WordPress Main Posts Loop Iterator
*/
class posts_loop implements Iterator {
public function __construct() { }
function rewind() {
rewind_posts();
}
function current() {
global $post;
return $post;
}
function key() {
global $post;
return $post->ID;
}
function next() {
the_post();
}
function valid() {
return have_posts();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment