Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active December 20, 2015 13:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjn/6140172 to your computer and use it in GitHub Desktop.
Save tomjn/6140172 to your computer and use it in GitHub Desktop.
If you're lucky enought o be running WordPress on PHP 5.5, you can use generators for your post loops like this
<?php
// with the above we can now do things like this:
foreach ( posts_loop() as $p ) {
get_template_part( 'content', 'page' );
comments_template( '', true );
}
// put this below in your functions.php:
// post_loop generator
function posts_loop() {
global $post;
while ( have_posts() ) {
the_post();
yield $post;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment