Skip to content

Instantly share code, notes, and snippets.

@hemusyl
Forked from gregrickaby/even_odd_classes.php
Created May 15, 2017 09:10
Show Gist options
  • Save hemusyl/03a26f5795795213d0e8cec1bd1e7430 to your computer and use it in GitHub Desktop.
Save hemusyl/03a26f5795795213d0e8cec1bd1e7430 to your computer and use it in GitHub Desktop.
Even odd classes in WordPress loop
<?php
// WP_Query Arguments
$args = array(
'order' => 'DESC',
'posts_per_page' => 5
);
// The Loop
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
// The count (must be inside the loop)
$count++;
$even_odd_class = ( ($count % 2) == 0 ) ? "even" : "odd";
?>
<div class="<?php echo $even_odd_class; ?>">
<p>Some content</p>
</div>
<?php endwhile; wp_reset_postdata(); ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment