WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Split posts into unequal rows (5 posts)

  1. amcio
    Member
    Posted 7 months ago #

    Hello people!
    I have a little problem. I need to split desired amount of posts (10) into four rows. Odd rows should have two posts, and even rows should have three posts. I have no idea how to make The Loop to make things work. I have no idea how to start. Can you help me out?

    Any help appreciated very much!

    <div class="odd-row">
    two posts here
    </div>
    <div class="even-row">
    three posts here
    </div>
    <div class="odd-row">
    two posts here
    </div>
    <div class="even-row">
    three posts here
    </div>
  2. shadez
    Member
    Posted 7 months ago #

    make use of a counter variable...

  3. alchymyth
    The Sweeper & Moderator
    Posted 7 months ago #

    you can use the loop counter $wp_query->current_post with some conditional statements;
    example::

    <?php
    while( have_posts() ) : the_post();
    	if( $wp_query->current_post%5 == 0 ) { echo "\n" . '<div class="odd-row">'; }
    	elseif( $wp_query->current_post%5 == 2 ) { echo "\n" . '<div class="even-row">'; }
    
    		//post content here//
    
    	if( $wp_query->current_post%5 == 1 || $wp_query->current_post%5 == 4 || $wp_query->current_post == $wp_query->post_count-1 ) { echo '</div>'; }
    endwhile;
    ?>
  4. amcio
    Member
    Posted 7 months ago #

    Ok thanks. Now I have something like that:

    <?php if ( have_posts() ) : ?>
    
        <?php $counter = 0; ?>
    
          <?php while ( have_posts() ) : the_post(); ?>
    
            <?php $counter++; ?>
    
            <?php echo $counter. ' — '; the_title(); echo '<br>'; ?>
    
            <?php if ($counter == 2) { echo 'odd 2<Br><br>'; } ?>
            <?php if ($counter == 5) { echo 'even 3<Br><br>'; } ?>
            <?php if ($counter == 7) { echo 'odd 2<Br><br>'; } ?>
            <?php if ($counter == 10) { echo 'even 3<Br><br>'; } ?>
    
          <?php endwhile; ?>
    
        <?php endif; ?>

    Can it be simplified more? ($counter == 2... $counter == 5...)

    ---
    Thanks @alchymyth, I'll try your version! <3

  5. amcio
    Member
    Posted 7 months ago #

    @alchymyth version works like a charm! Thank you boss

Reply

You must log in to post.

About this Topic