WordPress.org

Support

Support » Themes and Templates » Post Colors

Post Colors

  • Hi all;

    OK what I am trying to do is for my posts I would like for 1 post to be say grey background and the next to be white background. I want to set a class that skips posts. I tried to create a function that checks the “the_ID()” and writes a class in the post class section but I than realized that my posts are not in order so I cant check there if the post ID is either even or odd, anyway it would be great if someone could help me on this one, thanks! 🙂

    WiK

Viewing 2 replies - 1 through 2 (of 2 total)
  • Before the loop type

    <?php function post_style() {
    
    	static $post_count;
    
    	$post_count++;
    
    		if ($post_count % 2) {
    
    			echo "post_alt";
    
    		}
    
    		else {
    
    			echo "post";
    
    		}
    
    }
    
    ?>

    Instead of <div class=”post”> (or whatever else wraps your post)

    use

    <div class="<?php post_style(); ?>" id="post-<?php the_ID(); ?>">

    then end the loop with… (notice the else)

    <?php endwhile; ?>
    		<?php else : ?>
    		<?php endif; ?>

    to style this apply all styles to
    .post
    and
    .post_alt

    so in your case type:

    .post{ background: #fff;}
    .post_alt{ background: #eee;}

    Hey

    THANKS SO MUCH! WORKED LIKE A CHARM!

    WiK

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Post Colors’ is closed to new replies.