1

I was able to divide posts into two columns but I have a problem if the loop has an odd number of posts. If the loop has an odd number of posts, like 9, the right side has the most posts than the left side. What I want is if the loop has an odd number of posts, they favor the left side instead of the right side.

For example: this is what the loop outputs if it has 7 posts:

Post left | Post right
Post left | Post right
Post left | Post right
Post left | ----------

What I'm trying to accomplish:

Post left | Post right
Post left | Post right
Post left | Post right
--------- | Post right

The code used to split the loop:

<?php $row_start = 1; while ( $query->have_posts() ) : $query->the_post();?>
<?php if( in_array( get_the_ID(), $duplicates ) ) continue; ?>

<?php if( $row_start % 2 != 0) { ?>
<?php $left[] = get_the_ID(); ?>
<?php } else { ?>
<?php $right[] = get_the_ID(); ?>
<?php } ?>

<?php ++$row_start; endwhile; wp_reset_postdata();?>

Thanks for the help.

1
  • I think you can achieve this by using css last child . Just add float right to last child! – Rohit Kishore Aug 1 '16 at 5:41
1

You need to change the condition for populating the left array. First set the number of posts into a variable lets say $count and then use the below condition

if( $row_start % 2 != 0 && $row_start != $count )

This skips populating the left array for the last element.

2
  • Got it working: <?php if(in_array( get_the_ID(), $duplicates ) ) continue; ?> <?php if($row_start % 2 != 0 && $row_start != ($sum_total = $wp_query->found_posts - count($duplicates))):?> <?php $left[] = get_the_ID();?> <?php else:?> <?php $right[] = get_the_ID();?> <?php endif;?> – Gregory Schultz Aug 2 '16 at 5:49
  • Thank you for the help. I was able to complete my task with your help - wordpress.stackexchange.com/a/233419/8049 – Gregory Schultz Aug 2 '16 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.