PHP Multiples of 2, Show posts...not working (wordpress)

Posted on 16th Feb 2014 by admin

I have been using this code to show div.example with 6 li columns inside it, each li is a post with its info inside.

Once it gets to the 6th li it closes the div.example and starts a new one. This to enable me to divide them with a dotted line.

However, when it gets to the 4th row it prints an empty div.example because there are no li's to go in it, i just need it to stop once it gets to 24, now its stopping at 24 but printing one final div.example...

Really i need it to stop when there is no more li's to show, which it does however its printing this extra div.example and i dont know why.

You can see it here the big gap at the bottom is another div.example its showing with nothing inside it!


(only part of the whole page code as its the only bit that is not working correctly and isn't connected to anything else.)

Code: <?php

$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$postcnt = 0; query_posts('cat=3,4,5,6');
if (have_posts()) { ?>
<div class="example">
<ul class="content_cols">
<?php
while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true );?>

<li>
column here
</li>

<?php if($postcnt % 2 == 0) { ?>

<?php if($postcnt % 6 == 0) { ?>
</ul>
<div class="line"></div>
<div class="clearFix"><!--x--></div>
</div>

<div class="example">
<?php } ?>
<ul class="content_cols">
<?php } ?>
<?php
endwhile; ?>
</ul>
<div class="clearFix"><!--x--></div>
</div>
<?php
} ?>

The forum doesn't seem to be showing the first postcnt code properly, its the same as the one below just with 2 rather than 6

Any ideas?

Obviously very annoying as dont want that final div.example there at all as nothing is in it!!

Many thanks for any help

Other forums