Alternate post_class on each post

I need to have an alternating (even, odd…) class on posts to provide alternate highlights on a column. The best thing would be to attach this to the post_class() so that it’s on every instance of post_class(). Below is the code I have at this point to achieve this effect.

<?php 

// setting other variables for alternating categories
$style_classes = array('even', 'odd');
$style_counter = 0;
?>

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div class="<?php $k = $style_counter%2; echo $style_classes[$k]; $style_counter++; ?>">

<?php the_cotent(); ?>

</div>

<?php endwhile; ?>

<?php endif; ?>

Related posts

Leave a Reply

4 comments

  1. You should add the following code in functions.php:

    add_filter ( 'post_class' , 'my_post_class' );
    global $current_class;
    $current_class = 'odd';
    
    function my_post_class ( $classes ) { 
       global $current_class;
       $classes[] = $current_class;
    
       $current_class = ($current_class == 'odd') ? 'even' : 'odd';
    
       return $classes;
    }
    

    This ensures that all the odd posts on the page will have the class ‘odd’ and all the even posts will have the class ‘even’ just by using post_class() in your theme.

  2. This works, it passes in the additional class into post_class():

    <?php $c = 0; ?>
    <?php if (have_posts()) : ?>
        <?php while (have_posts()) : the_post(); ?>
            <div <?php post_class((++$c % 2 === 0) ? 'odd' : 'even'); ?>>
                <?php the_content(); ?>
            </div>
        <?php endwhile; ?>
    <?php endif; ?>
    

    EDIT: Here’s a way that creates a version of post_class() that will keep track of the count on the page. Now, it will use a new name, oddeven_post_class() but it does work as you want. All you need to do is drop this into functions.php:

    /* Drop this block into functions.php */
    class MyCounter {
        var $c = 0;
        function increment(){
            ++$this->c;
            return;
        }
        function oddOrEven(){
            $out = ($this->c % 2 === 0) ? 'odd' : 'even';
            $this->increment();
            return $out;
        }
    }
    $my_instance = new MyCounter();
    function post_class_oddeven() {
        global $my_instance;
        ob_start();
        post_class($my_instance->oddOrEven());
        $str = ob_get_contents();
        ob_end_clean();
        echo $str;
    }
    /* end block */
    

    So to call it, use post_class_oddeven() in your theme wherever you would call post_class()

  3. I have another solution if you want add a specific class to different content. For instance, only to loop:

    add_filter( 'post_class', 'my_post_class' );
    
    function my_post_class( $classes ) {
    
      if ( ! is_single() ) {
        global $wp_query;
    
        // Set "odd" or "even" class if is not single
        $classes[] = $wp_query->current_post % 2 == 0 ? 'even' : 'odd' ;
      }
    
      return $classes;
    }
    

    Because you don’t want add the “even” or “odd” class when displays a single content.

  4. You can do this with straight forward css. In twentyseventeen theme for example, where the loop is in a div “main”:

    #main .post {
        color: red;
    }
    
    #main .post:nth-of-type(2n) {
        color: blue;
    }
    

    will do the trick.