1

I want to create post layout in the following way:

+--------------------+
| 1 Full Width Post  |
+----------+---------+
| 2        | 3       |
+--------------------+
| 4 Full Width Post  |
+----------+---------+
| 5        | 6       |
+----------+---------+

So basically I want to use the post_class(). Say for full width it will be "full-width" class and for half width it will be "half-width" class.

I was able to add even/odd class by this method:

function alternating_post_class( $classes ) {
   global $wp_query;
   $classes[] = ( $wp_query->current_post % 2 === 0 ? 'odd' : 'even' ); 
   return $classes;
}
add_filter('post_class', 'alternating_post_class');

Any help would be greatly appreciated.

1 Answer 1

2

It just take a little bit more complicated logic:

function alternating_post_class($classes) {
  static $counter = 1;
  switch ($counter) {
    case 1:
      $classes[] = 'full-width';
      break;
    case 2:
    case 3;
      $classes[] = 'half-width';
      break;
  }
  $counter = ($counter == 3) ? 1 : $counter + 1;
  return $classes;
}
add_filter('post_class', 'alternating_post_class');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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