Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In a classic WordPress theme, initialize a counter before the Loop, calculate an odd or even class for each rendered post, and pass that class to post_class(). WordPress then prints your parity class together with its normal post classes on the post wrapper.

Classic-theme solution: add a counter to the Loop

Put the counter immediately before the specific post loop, increment it once after each post is rendered, and apply the result to the article (or another element that wraps one post). The following example labels the first item even because counting starts at zero.

<?php
$index = 0;
if ( have_posts() ) :
    while ( have_posts() ) :
        the_post();
        $parity_class = ( 0 === $index % 2 ) ? 'even' : 'odd';
        ?>
        <article <?php post_class( $parity_class ); ?>>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <?php the_excerpt(); ?>
        </article>
        <?php
        ++$index;
    endwhile;
endif;
?>

post_class() prints the complete class attribute for the current post and accepts one or more additional class names. See the official function reference. The Loop renders its template markup once for each post, as described in the Theme Handbook.

Make the first item odd instead

“Odd” and “even” are labels, so choose the convention your CSS and design use. To call the first post odd, start at one:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$index = 1;
$parity_class = ( 0 === $index % 2 ) ? 'odd' : 'even';

Keep the increment inside that loop and after the markup. The important rule is that the counter advances exactly once for every post that is actually output.

Style the generated classes

Scope your selectors to the list so the same class names do not affect unrelated elements:

.post-list .odd {
    /* Styles for alternating odd entries */
}

.post-list .even {
    /* Styles for alternating even entries */
}

For example, place post-list on the element containing the articles, while post_class() remains on each individual post wrapper. WordPress recommends using post_class() on that wrapper for dynamic post styling; the Post Formats documentation shows this pattern.

Choose the appropriate implementation

Approach Best when Trade-off
post_class() plus a PHP counter The parity class must exist in rendered HTML and the theme uses a classic PHP Loop You must maintain the counter in the correct loop
CSS :nth-child() Alternation is purely visual and the list’s direct-child markup is stable No semantic odd/even class is added; selectors depend on the DOM structure
Query Loop block class setting A block theme needs a static class on a Query Loop or its repeated template WordPress documentation does not provide a native per-post odd/even class control in that setting

When to use get_post_class() or the post_class filter

Inspect or modify classes as an array

get_post_class() returns the generated class names as an array, accepts a post ID or object, and runs the result through the post_class filter. It has been available since WordPress 2.7.0; custom taxonomy class names were added in 4.2.0. Consult the function reference before changing the array.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Centralize class logic with the filter

The post_class hook receives the generated classes, classes supplied to the function, and the post ID. Those arguments do not include a Loop index, so a filter alone cannot reliably know whether a post is first, second, or later. Use the explicit counter when position determines the class, or provide your own dependable position state.

Block themes and the Query Loop

The Query Loop block repeats a Post Template for queried posts and offers an Additional CSS Class(es) field in its advanced settings. The Query Loop documentation supports adding a static class there, but does not establish a built-in control that emits alternating classes for each repeated post. If you need per-item parity in a block theme, assess whether a CSS structural selector meets the visual requirement or whether the repeated markup needs custom code.

Implementation checklist

  • Declare the counter before the particular list whose alternation should restart.
  • Increment it once per post that the loop renders.
  • Apply post_class() to the post container, not the outer list container.
  • Decide whether the first visible item is named odd or even, then keep the CSS convention consistent.
  • Use a separate counter for separate post lists when each list should begin its own alternation.
  • Keep custom class names simple; WordPress escapes additional classes when it builds the class attribute.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshooting parity problems

The first item has the wrong label

Check whether your counter starts at zero or one. Reverse the ternary condition or change the initial value; the visual result is identical as long as your selectors match.

Alternation resets or skips unexpectedly

Make sure the counter belongs to the intended loop, is not reinitialized inside the loop, and is incremented only after a post is output. Conditional markup that bypasses a post should advance the counter only if that post was rendered.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Classes appear on the wrong element

Move post_class() onto the element wrapping one complete post, commonly an article or div. Putting it on the list container gives every item the same positional class.

Custom classes are missing

Verify that the value passed to post_class() is a non-empty string such as odd or even, and inspect the generated HTML. The function should still output WordPress’s standard classes alongside your extra class.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.