0

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>
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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