WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to alternate the background on a theme page that is displaying a custom loop:

$args = array( 'post-type' => 'portfolio', 'posts_per_page' => 10 );
                        $loop = new WP_Query( $args );
                        $count = 0;
                        while ($loop->have_posts() ) : $loop->the_post();

<div class="wrapper <?php if(++$count%2==0) echo 'grey-box'; else echo 'white-box'; ?>">

<div class="portfolio-picture <?php if(++$count%2==0) echo 'portfolio-grey-box'; else echo 'portfolio-white-box'; ?>">

                        <?php $videocode = get_post_meta($post->ID, 'video-embed-code', true); //is the custom field being used?

                        if ($videos) {

                            echo $videocode;



                        if ( has_post_thumbnail() ) {




                    <div class="portfolio-description">

                        <p><strong><?php the_title(); ?></strong></p>

                        <p><?php the_content(); ?></p>


                <div class="clear"></div>

                <?php endwhile; ?>

But the styles don't seem to change. Am I missing something?

share|improve this question
up vote 2 down vote accepted

You're incrementing $count twice in every iteration of the loop, the first if will always be false and the second will always be true.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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