Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6704207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:15:43+00:00 2026-05-26T07:15:43+00:00

This code adds a unique class to every first/second/third multiple in the WordPress loop:

  • 0

This code adds a unique class to every first/second/third multiple in the WordPress loop:

<?php
$style_classes = array('first','second','third');
$style_index = 0;

if (have_posts()) : while (have_posts()) : the_post(); ?>
<div <?php $k = $style_index%3; echo "class=post&nbsp;$style_classes[$k]"; $style_index++; ?>>test</div>

So the first post has a class of first, the second a class of second, the third a class of third, and then it resets with the fourth post having a class of first, the fifth post a class of second, etc etc.

Is there a way to also make only the first three posts have an additional class called “special”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:15:44+00:00Added an answer on May 26, 2026 at 7:15 am

    You should not compress together so much code into one line.

    Instead, make one statement per line, that keeps you flexible to add stuff, like your special class:

    <?php
    $style_classes = array('first','second','third');
    $style_index = 0;
    
    if (have_posts()) : 
        while (have_posts()) : 
            the_post();
            $style_index = $wp_query->current_post;
            $classes = array('post');
            $classes[] = $style_classes[$style_index % 3];
            if ($style_index < 3) $classes[] = 'special'
            $class = sprintf('class="%s"', implode(' ', $classes));
    ?>
    <div <?php echo $class?>>test</div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code selects cell values in MySQL and manually adds them to PHP variables:
I have this PHP code that adds a set of nodes to an XML
this code always returns 0 in PHP 5.2.5 for microseconds: <?php $dt = new
I've tried this a couple ways, the first is have a class that implements
This code adds the text liked inside div.gdt-starrating : $(a.gdt-starrating).live(click, function() { $(div.gdt-starrating).text(liked); });
In the following code, person.Children contains 3 other Person objects. This code adds the
I have a weirdest thing, in this peace of code a browser adds tags
In this code, I create an array of strings 1 to 10000: array_of_strings =
i have this code : <?php echo $_POST['id-input']; $time = time(); $ip=$_SERVER['REMOTE_ADDR']; $userid_c =
This code in JS gives me a popup saying i think null is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.