Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active February 21, 2020 06:35
Show Gist options
  • Select an option

  • Save tomjn/6140909 to your computer and use it in GitHub Desktop.

Select an option

Save tomjn/6140909 to your computer and use it in GitHub Desktop.

Revisions

  1. tomjn revised this gist Aug 2, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    $pages = new query_loop( array(
    'post_type' => 'page'
    ));
    foreach( $posts as $id => $post ) {
    foreach( $pages as $id => $post ) {
    the_title();
    // etc...
    }
  2. tomjn revised this gist Aug 2, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@
    // etc...
    }


    // put this below in your functions.php:
    /**
    * WordPress Main Posts Loop Iterator
    */
  3. tomjn created this gist Aug 2, 2013.
    43 changes: 43 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    <?php

    $pages = new query_loop( array(
    'post_type' => 'page'
    ));
    foreach( $posts as $id => $post ) {
    the_title();
    // etc...
    }

    /**
    * WordPress Main Posts Loop Iterator
    */
    class query_loop implements Iterator {
    public function __construct( $args = array() ) {
    $this->query = new WP_Query( $args );
    }

    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;
    }
    }
    }