0

I have an array which holds a list of colours.

<?php $colours = array("pink", "red", "blue", "green", "yellow", "grey", "cyan"); ?>

I then have a Wordpress loop running to display posts. Is there a way that each post would have a value taken from the array and then the one after would have the other value?

For example:

First post: <div class="pink"></div>

Second post: <div class="red"></div>

3 Answers 3

1

if it's a loop like this

<!-- Start the Loop. -->
 <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <div class="post">


    <!-- Display the Title as a link to the Post's permalink. -->

    <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

you can simply add a counter like this

<!-- Start the Loop. -->
<?php $count = 0; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<div class="<?php echo $colours[$count % count($colours)]; ?>">
<?php $count++; ?>

<!-- Display the Title as a link to the Post's permalink. -->

<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
Sign up to request clarification or add additional context in comments.

Comments

0
<?php
foreach($colours as $color) {
?>
<div class="<?php echo $color; ?>"> </div>
<?php } ?>

2 Comments

Hi, this would return a list of divs with the different colours. What i need to do is for each Wordpress post to add a colour. Does that make sense? Sorry, i should've been more precise in my question.
for that i think you can take different varibles and put different color for odd and even post , code will be not feasible but you may achive your goals
0

You can make a decision over key. Check it if odd then it will go to the 1st div else even it will go to the 2nd div. Wish it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.