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'd like to have a query that returns posts grouped five by five.

Right now I have a simple query loop like this one :

<?php if( $query->have_posts() ) : ?>
    <ul class="posts">
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <li>
                <?php the_title(); ?>
            </li> 
        <?php endwhile; ?>
    </ul>
<?php endif; ?>

It outputs :

<ul class="posts">
    <li>post 1</li>
    <li>post 2</li>
    <li>post 3</li>
    <li>post 4</li>
    <li>post 5</li>
    <li>post 6</li>
    <li>post 7</li>
    <li>post 8</li>
    <li>post 9</li>
    <li>post 10</li>
</ul>

Instead, what I'd like to have is :

<ul class="posts">
    <li>post 1</li>
    <li>post 2</li>
    <li>post 3</li>
    <li>post 4</li>
    <li>post 5</li>
</ul>

<ul class="posts">
    <li>post 6</li>
    <li>post 7</li>
    <li>post 8</li>
    <li>post 9</li>
    <li>post 10</li>
</ul>

How can this be done ?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

you can create a simple counter and condition:

 <?php $count = 0; ?>
    <?php if( $query->have_posts() ) : ?>
        <ul class="posts">
            <?php while ( $query->have_posts() ) : $query->the_post();
            if ($count < 5){ 
                ?>
                <li>
                    <?php the_title(); ?>
                </li> 
                <?php
            }else{
                echo '</ul><ul class="posts">';         
            ?>
                <li>
                    <?php the_title(); ?>
                </li> 
                <?php
                $count = 0;
            }
            $count = $count + 1;
            <?php endwhile; ?>
        </ul>
    <?php endif; ?>
share|improve this answer
    
Hehe!! You beat me by seconds. :) –  Hameedullah Khan Jun 29 '11 at 10:53
    
that creates a group of five only for the first five posts. I'm trying to make groups of five for any number of posts. –  mike23 Jun 29 '11 at 10:59
    
@mike23, my bad , i added a better solution, which simply counts to 5 each time and if you want to change the number just change 5 to whatever you want –  Bainternet Jun 29 '11 at 11:07
    
actually your solution works better. –  mike23 Jun 29 '11 at 14:00
    
@mike23, glad i could help. –  Bainternet Jun 29 '11 at 18:20
add comment

Did you try this?

<?php if( $query->have_posts() ) : ?>
    <ul class="posts">
        <?php $count = 0; ?>
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <li>
                <?php the_title(); ?>
            </li> 
        <?php 
           $count++; 
           if ( ( $count % 5 ) == 0 && $query->have_posts() ) {
        ?>
    </ul>
    <ul class="posts">
        <?php
           }
        endwhile; ?>
    </ul>
<?php endif; ?>
share|improve this answer
    
thanks that works. –  mike23 Jun 29 '11 at 11:09
    
sometimes a last empty item (<ul></ul>) is created. For example if you have 9 posts and show groups of 3 ($count % 3), it actually creates 4 groups, three filled with the posts and one empty one at the end. Do you have an idea how to fix that ? –  mike23 Jun 29 '11 at 13:52
    
check now, its fixed –  Hameedullah Khan Jun 29 '11 at 14:55
add comment

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.