Here is my loop
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php $i = 0; while ( have_posts() ) : the_post(); ?>
<?php if ($i++ == 0) : ?>
<div class="entry-content-main">
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<?php else: ?>
<div class="entry-content-rest">
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
and my css
.entry-content-rest {
width: 48.5%;
float: left;
border: 1px solid #ddd;
border-radius: 7px;
}
This displays my content as follow
Post1
Post2 Post3
Post4 Post5
My problem is, say post 2 is longer (have more contents) than post 3, then my columns is displayed way wacky like this
Post1
Post2 Post3
open Post4
Post5 Post6
Any suggestions to fix this
3 Answers
you could rewrite the full code and use the build-in loop counter $wp_query->current_post
to fix the css classses and add a class for the first post per row to clear the float to prevent the ‘wacky sticking’;
example:
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php /* Start the Loop */ ?>
<div class="entry-content-<?php echo (( $wp_query->current_post == 0 ) ? 'main' : 'rest' ); if( $wp_query->current_post%2 == 1 ) echo ' left-post'; ?>">
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<?php endwhile; ?>
additional css:
.left-post { clear: left; }