While loop, wrap items in a count in a separate div (index.php)

I have the following so far:

<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>

                <?php if ($count < 5) : ?>
                    //four posts here, all them wrapped in one div, these will also have different markup
                    <?php the_title();?>



                <?php else : ?>
                   //remaining six posts here, all of them wrapped in one div, these will also have different markup
                    <?php the_title();?> blah
                    <?php the_excerpt();?>

                <?php endif; ?>


            <?php endwhile; ?>

I want to wrap the first 4 in a ‘<div class="top"></div>‘ and the bottom 6, I want all of them wrapped in a ‘<div class="bottom"></div>

Read More

Any tips of how this can be done using the while loop would be greatly appreciated

Related posts

2 comments

  1. Try the below snippet.

    <?php 
    $count = 0;
    
    while (have_posts()) : the_post(); $count++; 
    
        if ( $count == 1 ) echo '<div class="top">';
        if ( $count > 5 ) echo '</div><div class="bottom">';
    
        the_title();
        if ( $count > 5 ) the_excerpt();
    
    endwhile;
    
    echo "</div>"
    ?>
    
  2. This might help you.

            <?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
    
                <?php if ($count < 5) : ?>
                    //four posts here, all them wrapped in one div, these will also have different markup
                   <div class="top">
                    <?php the_title();?>
                   </div>
    
    
    
                <?php else : ?>
                   //remaining six posts here, all of them wrapped in one div, these will also have different markup
                    <div class="bottom">
                    <?php the_title();?> blah
                    <?php the_excerpt();?>
                    </div>
                <?php endif; ?>
    
    
            <?php endwhile; ?>
    

Comments are closed.