Query_posts wrap every 6 posts together

47 Views Asked by At

I'm using query_post to call all posts within a custom post type I have created called 'partners'.

What I would like so it to wrap the posts in a div in groups of 6's. For example:

<div class="item">
   <img src="#"/>
   <img src="#"/>
   <img src="#"/>
   <img src="#"/>
   <img src="#"/>
   <img src="#"/>
</div>
<!-- 6 images/posts wrapped -->
<div class="item">
   <img src="#"/>
   <img src="#"/>
   <img src="#"/>
   <img src="#"/>
   <img src="#"/>
   <img src="#"/>
</div>
<!-- 6 images/posts wrapped -->

This is my code so far:

<?php
                query_posts('post_type=partners');
                if (have_posts()) : while (have_posts()) ;

                $posts = the_post();
                if( $posts ): ?>
                    <? $lastIndex = count($posts) - 1; ?>
                    <? foreach($posts as $index => $post) : ?>
                        <? setup_postdata($post); ?>

                        <? if($index % 6 === 0) { ?>
                            <div class="item <?=$index === 0 ? 'active' : '' ?>">
                        <? } ?>
                        <div class="car-part-logo">
                            <? the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
                        </div>
                        <? if(($index + 1) % 6 === 0 || $index === $lastIndex) { ?>
                            </div>
                        <? } ?>
                    <? endforeach; ?>
                    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>


                <?php endwhile; ?>
                <?php endif; ?>
                <?php wp_reset_query(); ?> 

However I get the following error: unexpected T_ENDWHILE

2

There are 2 best solutions below

0
On BEST ANSWER

I managed to rebuild the code based on this previous question. I altered my code to work with 6 items instead of 3.

<?php query_posts('post_type=partners'); ?>
                <?php $variable=0;?>
                <div class="item active">
                    <?php while ( have_posts() ) : the_post(); ?>
                    <?php if(($variable+1)<7){ ?>
                        <div class="car-part-logo">
                            <?php the_post_thumbnail('full', array('class' => 'img-responsive'));   ?>
                        </div>
                    <?php $variable+=1; ?>
                    <?php }else{ ?>
                    <?php $variable=1; ?>
                </div>
                <div class="item">
                    <div class="car-part-logo">
                        <?php the_post_thumbnail('full', array('class' => 'img-responsive'));   ?>
                    </div>
                <?php }?>
                <?php endwhile; ?>
                </div>
            <?php wp_reset_query(); ?> 
0
On

try this code <?php

            query_posts('post_type=partners');

            while ( have_posts() ) : the_post();

            //if (have_posts()) : while (have_posts()) ;

            $posts = the_post();
            if( $posts ): ?>
                <? $lastIndex = count($posts) - 1; ?>
                <? foreach($posts as $index => $post) : ?>
                    <? setup_postdata($post); ?>

                    <? if($index % 6 === 0) { ?>
                        <div class="item <?=$index === 0 ? 'active' : '' ?>">
                    <? } ?>
                    <div class="car-part-logo">
                        <? the_post_thumbnail('full', array('class' => 'img-responsive')); ?>
                    </div>
                    <? if(($index + 1) % 6 === 0 || $index === $lastIndex) { ?>
                        </div>
                    <? } ?>
                <? endforeach; ?>
                <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>

            <?php endif; ?>
            <?php endwhile; ?>

            <?php wp_reset_query(); ?>