Forums

[resolved] Add class to every third post? (3 posts)

  1. UlukaiThemeDesign
    Member
    Posted 1 year ago #

    How do I add a class to every third post,
    similar to how even/odd/alt is added to posts?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    You can try it with a counter. Here is a very simple loop that gives every third post a class of "third-post";

    <?php $counter =0; ?>
    <?php while (have_posts()) : the_post(); ?><!-- begin of loop -->
    <?php
      ++$counter;
      if($counter == 3) {
        $postclass = 'class="third-post"';
        $counter = 0;
      } else { $postclass = ''; }
    ?>
    <div id="post-<?php the_ID(); ?>" <?php echo $postclass; ?>>
    <?php the_content('Read the rest of this entry ยป'); ?>
    </div>
    <?php endwhile; ?><!-- end of loop -->
  3. UlukaiThemeDesign
    Member
    Posted 1 year ago #

    Thanks a bunch!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags