How to add css class on every third post in wordpress?

Tags: php,css,wordpress

Problem :

I want to add a css class to every third post display on page. I have tried this code given below:

 <?php if (have_posts()) : ?>
 <?php $c = 0;
     while (have_posts()) : the_post(); $c++;
        if( $c == 3) {
            $style = 'third';
            $c = 0;
        else $style='';
     <div <?php post_class($style) ?> id="post-<?php the_ID(); ?>">

Solution :

try to change

 while (have_posts()) : the_post(); $c++;
 if( $c == 3) {
        $style = 'third';
        $c = 0;
    else $style='';


 while (have_posts()) : the_post();
      if( $c % 3 == 0 )
         $style = 'third';
         $style = '';

and make increment of varible $c++; before the while loop ends

I feel

<?php post_class($style) ?> id="post-<?php the_ID(); ?>">

is working, if not than change it to

<?php class = "<?php $style; ?>" id="post-<?php the_ID(); ?>">

    CSS Howto..

    How to change the background color of an input field when text is entered?

    Problems with properly simulate a border to a “ribbon” with css. How do I correct it?

    How to position an icon in a metro-like web GUI? (images included)

    How to size relative div which haven't a content?

    Why does this qtip2 show blank content on 2nd click?

    How to enable HTML content on top of Flash content without the frame rate dropping?

    How to make a image resize to fit a div

    how to add an overlaying color over an image with css

    DotNetNuke Skinning: How do I use\add skin.css?

    how to hide the content of the div in css

    How to delete HTML Elements in an iFrame using JavaScript

    How does one make one table column header taller than the rest?

    How to remove all pseudo selectors from a CSS selector string using Javascript?

    How to create a faded background image?

    How do I change ASP.NET App_Themes based on what Skin is selected in RadSkinManager?

    How to make 2 backgrounds “stick” to the sides of a centered div

    How to control CSS cache?

    Why does overflow hidden affect height and how can I fix it in this example?

    How to change font size of the first option tag in a dropdown menu (select)?

    How to queue CSS3 animations when shown after page scroll (JSFiddle provided)

    How to use a background image in Webfolio WordPress theme without breaking CSS header?

    How to get a fixed DIV to stay on top of an absolute?

    CSS: How can I set image size relative to parent height?

    How to make content of invisible except for the left column?

    How to add javascript expression's output to my CSS?

    How can I add a css class to an updatepanel in ASP.Net?

    How to render no-table HTML form into two columns with CSS using classes?

    How Do I Use Css To Select A Area Of Code

    How to make interactive logo design without flash?

    How to give different content to every td in the table