WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Split posts into unequal rows

[Resolved] Split posts into unequal rows

  • 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>
Viewing 4 replies - 1 through 4 (of 4 total)
  • make use of a counter variable…

    Michael

    @alchymyth

    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;
    ?>

    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

    @alchymyth version works like a charm! Thank you boss

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Split posts into unequal rows’ is closed to new replies.
Skip to toolbar