Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am displaying posts in a grid (4 posts wide by 2 posts high). I am using a table. In order to get the content to display on the second row, I am using two wp_queries (one on each row) because I can't figure out how to make the second row while being inside the loop.

I'm trying to figure out if I can achieve this same layout with a single query instead of having to do two of them. How could I achieve this?

Here is the part of my code I'm referring too:

<table>
        <tr>
        <?php
            $args = array (
                posts_per_page => 4,
                post_type => 'post',
                orderby => 'meta_value_number',
                meta_key => 'episode',
                order => 'ASC',
                post_status => array('publish, future')
                );
            // The Query
            $query = new WP_Query( $args );

            // The Loop
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post(); 
                    $pID = $post->ID ?>
                <td>
                    <form id="episode-<?php echo $pID; ?>" name="episode-<?php echo $pID; ?>" method="post" action="">
                        <div class="gallery-thumb">
                            <a onclick="document.getElementById('episode-<?php echo $pID; ?>').submit();"><?php the_post_thumbnail(); ?></a>    
                        </div>
                        <input type="hidden" name="pID" value="<?php echo $pID; ?>" />
                    </form>
                    <div class="gallery-text">
                        Episode <?php the_field('episode'); ?><br />
                        "<?php the_title(); ?>"<br />
                        <?php 
                        if (get_post_status() == 'publish') {
                            echo get_the_time('M d, Y');
                        } else {
                            echo 'Premieres '; echo get_the_time('M d');
                            }
                        ?>
                    </div>
                </td>   
            <?  }
            } else {
                // no posts found
            }
            /* Restore original Post Data */
            wp_reset_postdata();
            ?>
        </tr>
        <tr>
            <?php
            $args = array (
                posts_per_page => 4,
                offset => 4,
                post_type => 'post',
                orderby => 'meta_value_number',
                meta_key => 'episode',
                order => 'ASC',
                post_status => array('publish, future')
                );
            // The Query
            $query = new WP_Query( $args );

            // The Loop
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) {
                    $query->the_post(); 
                    $pID = $post->ID?>
                <td>
                    <form id="episode-<?php echo $pID; ?>" name="episode-<?php echo $pID; ?>" method="post" action="">
                        <div class="gallery-thumb">
                            <a onclick="document.getElementById('episode-<?php echo $pID; ?>').submit();"><?php the_post_thumbnail(); ?></a>    
                        </div>
                        <input type="hidden" name="pID" value="<?php echo $pID; ?>" />
                    </form>
                    <div class="gallery-text">
                        Episode <?php the_field('episode'); ?><br />
                        "<?php the_title(); ?>"<br />
                        <?php 
                        if (get_post_status() == 'publish') {
                            echo get_the_time('M d, Y');
                        } else {
                            echo 'Premieres '; echo get_the_time('M d');
                            }
                        ?>
                    </div>
                </td>   
            <?  }
            } else {
                // no posts found
            }
            /* Restore original Post Data */
            wp_reset_postdata();
            ?>

        </tr>
    </table>
share|improve this question

1 Answer 1

up vote 2 down vote accepted

Get all of your posts in one query, then count the iterations of your loop.

Pseudo-code follows.

if ( $query->have_posts() ) {
$i = 0;
                while ( $query->have_posts() ) {

// If $i is 5 (5th post), end one row and start the next
if (++$i == 5) {
echo '</tr><tr>';
}
// the rest of your loop output
share|improve this answer
    
Brilliant! Thank you. –  Eckstein Aug 16 '13 at 22:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.