I am using the article tag with the following code:

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix span4 '); ?> role="article">

and I would like to add "odd" and "even" to the current post_class.

I found the following code on other site:

<?php echo (++$j % 2 == 0) ? 'evenpost' : 'oddpost'; ?>

Why it isn't working with my code?

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix span4 <?php echo (++$j % 2 == 0) ? 'evenpost' : 'oddpost'; ?>'); ?> role="article">
  • 1
    is $j actually set anywhere? – vancoder Apr 2 '13 at 18:40
  • Thanks @vancoder. Not that I know of, but if I use <?php echo (++$j % 2 == 0) ? 'evenpost' : 'oddpost'; ?> alone it works. The thing is when I add this code inside the <?php post_class because it's php inside of php? – Johann Apr 2 '13 at 18:44
  • I see. See answer. – vancoder Apr 2 '13 at 18:51
  • This is a basic PHP question, not a WordPress question. The asker has demonstrated that they do not understand the needed basics, e.g. joining strings and basic variables, else we would not be seeing <?php tags inside a string literal – Tom J Nowell Apr 2 '13 at 21:37

Try this (untested):

<?php $zebra = (++$j % 2 == 0) ? 'evenpost' : 'oddpost'; ?>

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix span4 ' . $zebra); ?> role="article">

You just need to place theodd/even class into a variable before passing it to post_class.

  • There's an extra ' in your code, in the <article> line, after $zebra and before the ). – Pat J Apr 2 '13 at 18:55
  • Thanks VanCoder your code works perfectly too. I'll just mark @s_ha_dum answer as the solution since he provided 2 solutions (via a variable and writing the code directly). Thanks a lot! – Johann Apr 2 '13 at 19:24
  • 1
    Fair enough.... – vancoder Apr 2 '13 at 19:25
  • @vancoder Although I somewhat fear to make myself a laughing stock ... why the heck is there a zebra in your code? :-/ – tfrommen Apr 2 '13 at 20:15
  • 2
    @tf I assume Johann is attempting a form of zebra striping – vancoder Apr 2 '13 at 20:43

That fact that this concerns post_class is a pretty thin reason to call the question "WordPress related". Mostly it is just PHP but I'll bite.

You can make it easy by setting a variable first.

$additional_class = (++$j % 2 == 0) ? 'evenpost' : 'oddpost'; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix span4 '.$additional_class); ?> role="article">

But it you are careful you can write that code directly into the post_class parameter.

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix span4 '.((++$j % 2 == 0) ? 'evenpost' : 'oddpost')); ?> role="article">

Per Chip's prodding, I cooked up a post_class filter based solution:

function even_odd_post_class($classes) {
  global $wp_query;
  $classes[] = ($wp_query->current_post % 2 == 0) ? 'evenpost' : 'oddpost';
  return $classes;

Passing around that $j variable in the original implementation in the question could be tricky and require a global or an object based callback with a static variable, so I don't know if I'd consider it neater. If we can use the already global $wp_query counter, a filter based solution is not hard to manage. I still don't know if I'd call it neater since it will run on potentially numerous indexes as well as single posts pages and would require additional internal logic to remove. Directly editing the code into the appropriate template has at least a chance of being the cleaner more efficient solution. Which is best probably depends on the context, but there are three options now.

  • Wouldn't a post_class filter be a better implementation? – Chip Bennett Apr 2 '13 at 19:16
  • Hi @ChipBennett, you are totally right, there's a thin line because I was not sure if there was a Wordpress filter I could use in order to implement these classes. BTW, your code works so I will mark this as the solution. – Johann Apr 2 '13 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.