Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteIn 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:
#1 Best Overall
$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:
Rank #2
- Used Book in Good Condition
.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.
Quick wins for a faster PC:
Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →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.
Rank #4
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
oddoreven, 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.
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.
Best Value
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.
Quick Recap
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.

