I’m assuming you want the even class added to alternate posts? You could replace the <dl>
block contents with something like this:
<?php
if ($stripe) {
echo '<div class="even">';
$stripe = false;
}else{
echo '<div class="odd">';
$stripe = true;
}?>
/* All the remaining loop stuff goes here starting with <dt class=dienst"> and ending with... */
<?php the_content(); ?></dd></div>
Tweak as needed for your exact needs. To be complete, you should add $stripe = false;
before the while
loop. This starts with odd first, simply set to true instead for even first.
to add the ‘odd/even’ class to the <dl>
, try:
<dl class="<?php echo ($loop->current_post%2 == 0?'odd':'even'); ?>">
I am trying to accomplish the same thing with my custom posts, but can’t seem to translate those answers into my code. I’d like the odd-numbered posts to be styled differently than the even-numbered posts. Here’s my code:
<?php $args = array( 'post_type' => 'book', 'posts_per_page' => 3 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="entry-content">';
the_title();
the_content();
echo '</div>';
endwhile; ?>
I guess I need to add an additional class (.odd or .even) to my div. Right?
TIA!
@meredith
just ‘odd/even’ for three posts might not be enough;
(I already posted some related suggestion in your other topic: http://wordpress.org/support/topic/how-to-display-multiple-custom-post-types-in-separate-divs-on-a-page-template?replies=8#post-3512946
I’ll repost a precise suggestion to match your code there.)
if you insist on using just ‘odd/even’, here is the edited div:
echo '<div class="entry-content' . ($loop->current_post%2 == 0?' odd':' even') . '">';
@alchymyth
Thanks so much, that worked perfectly! 🙂