WordPress.org

Ready to get started?Download WordPress

Forums

Post Colors (3 posts)

  1. brianwik
    Member
    Posted 6 years ago #

    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

  2. Webmeba
    Member
    Posted 6 years ago #

    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;}

  3. brianwik
    Member
    Posted 6 years ago #

    Hey

    THANKS SO MUCH! WORKED LIKE A CHARM!

    WiK

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.