That seems overly complex to sort the posts into columns. You should be able to achieve the results you want with a some simple logic.
- Get the index of the current post.
- Check the result of the modulus operator applied to the index.
- Assign the appropriate column class based on the modulus result.
<?php if (have_posts()) : ?>
<?php while(have_posts()) : the_post();
if ($wp_query->current_post % 2) {
$column_class=\'left-column\';
} else {
$column_class=\'right-column\';
}
?>
<div id="<?php echo $column_class; ?>">
<!-- your content goes here -->
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; else: ?>
<!-- no posts to display -->
<?php endif; ?>
Other Considerations
You should also consider not using query_posts()
. The following adjustments should provide the same result.
Switch query_posts()
for WP_Query
.
$query = new WP_Query( array( \'order\' => \'ASC\' , \'orderby\' => \'title\' , \'posts_per_page\' => -1 , \'post_type\' => \'stockist_directory\', \'regions\' => \'metropolitan\' ) );
Then adjust The Loop to use $query
. Also note that $wp_query->current_post
has changed to $query->current_post
.
<?php if ($query->have_posts()) : ?>
<?php while($query->have_posts()) : $query->the_post();
if ($query->current_post % 2) {
$column_class=\'left-column\';
} else {
$column_class=\'right-column\';
}
?>
After the loop reset the post data like this.
<?php wp_reset_postdata(); ?>