I’m working on wordpress query_posts. I want to show 12 posts on the index page,3 items in a row. So I want to have a “clear:both” css on the first item of each row. How can I do that please?
<?php query_posts(array('showposts' => 9, 'post_parent' => $post->ID, 'post_type' => 'page', 'order' => 'ASC')); ?> <div> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <div> <!-- clear class on each 4th item --> <h2><?php the_title(); ?></h2> <?php the_content(); ?> </div> <?php endif; ?> </div> <?php wp_reset_query(); ?>
I added 2 lines.
<?php $i = 0; $attr = " class='clear_float'"; ?>
and
<div<?php if(($i++)%3 == 0) {echo $attr;} ?>> <!-- clear class on each 4th item -->
===== UPDATED =====
To add 3rd item class, I would suggest adding class to all items, for simplicity and even more control
To do so, before the loop:
Inside the
div
in the loop:So that, for each line, the first item has class =
item-1
, the 3rd item has class =item-3
Initialize your counter to be zero. Increment it by one, divide by 3 and check if the remainder is 0 (implying it’s an even multiple of 3), in which case you set your clearing class/style. In less terse code: