• how could I add a specific class to every second and third post in a group of 4 posts? I want the posts to be like this:

    A
    B C
    D
    E F
    G

    Obviously B,C and E,F would have the class
    how could I achieve this?

Viewing 2 replies - 1 through 2 (of 2 total)
  • add a specific class to every second and third post in a group of 4 posts?

    assuming that the post’s div has post_class(), this is doable with using the $wp_query->current_post of a default loop, in a filter function added to functions.php of your theme:

    function special_recurrent_post_class($classes) {
      if( is_home() ) {
      global $wp_query;
      $extra_classes = array('','specific','specific','');
      $classes[] = $extra_classes[$wp_query->current_post%count($extra_classes)];
      }
      return $classes;
    }
    add_filter('post_class', 'special_recurrent_post_class');

    (example restricts this to posts on the home page or posts page; for other, you need to change the coditional tag is_home() to something else; http://codex.wordpress.org/Conditional_Tags

    watch out:
    the code is for a group of 4;
    your example shows a group of 3 – be sure about what you want.

    if this is not what you want, please post the existing code of the template – see forum guidelines http://codex.wordpress.org/Forum_Welcome#Posting_Code

    Thread Starter coke

    (@coke)

    wow, thank you so much! It works like a dream!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘add a class to every second and third post’ is closed to new replies.