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 8516547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:27:35+00:00 2026-06-11T05:27:35+00:00

I will start saying with I’m really a noob on PHP and I need

  • 0

I will start saying with I’m really a noob on PHP and I need to solve a problem, I know the logic, however in the syntaxis I’m dead.

Here is the case.

I need to create a wordpress loop, which will display images with a span class with 3 different colors, 1 per loop, so it goes, 0 = green, 1 = blue, 2 = pink.

Since I have 3 colors a X numbers of outputs, I think about creating an array from 0 to 2, an assign a value according the colors I described, then create a conditional that will go as, if i > 2, then i = 0.

With this I will just echo the array value on my span class, that should do the trick.

What I need help with is creating the syntaxys for this, so far, this is what I have:

<?php while ( have_posts() ) : the_post(); $i = 0 ?>
 <?php 
    $array = array(
      0 => 'green', 
      1 => 'blue', 
      2 => 'pink',
    ); 
  ?>
 <span class="<?php echo $array ?>;">Title</span>

 <?php
  $i++;
 ?>
 <?if ($i > 2) { $i=0; } endif; ?> 
<?php endwhile; ?> 

I’m missing the part where I should compare the counter i with the array.

I would appreciated any help!

Thanks in advance.

  • 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-06-11T05:27:36+00:00Added an answer on June 11, 2026 at 5:27 am

    There were a few problems with this code (as well as some suggestions).

    • You were defining $i = 0; inside the while loop meaning it was always == 0
    • You are trying to print the $array instead of one item

    How to fix:

    • Move $i = 0; outside the while loop (so it’s defined before the loop)
    • Use echo $array[$i] to echo the item id you’re looking for
    • Use the modulo operator to get the remainder of $i / 3 and use that as your index

    You could also

    • Define the array on one line $colors = array('green', 'blue', 'pink'); which will behave the same – this works as PHP uses a default index starting at 0
    • Use variables like $colors instead of $array to make it clearer what’s going on
    • Reduce the amount of <?php tags to keep things clearer
    • Define the array (now called $colors) outside of the loop

    See the complete code below

    <?php 
      $colors = array(
        0 => 'green', 
        1 => 'blue', 
        2 => 'pink',
      ); 
    
      $i = 0;
      while ( $loop->have_posts() ) : $loop->the_post();    
    ?>
    <span class="<?php echo $colors[$i % 3] ?>;">Title</span>
    <?php
      $i++;
      endwhile;
    ?>
    

    And a further simplification

    <?php 
      $colors = array('green', 'blue',  'pink'); 
      $i = 0;
      while ( $loop->have_posts() ) : $loop->the_post();    
    ?>
        <span class="<?php echo $colors[$i % 3] ?>;">Title</span>
    <?php
        $i++;
      endwhile;
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will start by saying that I don't think what I want can be
As we all know Java program will start executing from the public static void
Ok, so I will start by saying that I am new to all this
Let me start by saying I know about position() but I can't seem to
Ok I will start by saying I'm fairly new to programing android applications. That
First let me start of saying I know absolutely nothing about c++ and I
I will start by saying I've read this topic: C++ Return reference / stack
Let me start by saying that I know it is a protected method and
Let me start off by saying I'm a huge Git (and Ubuntu) noob. I
Let me start off by saying I know how to use CausesValidation and ValidationGroup

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.