Jacob T.
Jacob T.

Reputation: 23

Choosing Hex Color Code From Multiple Options

Trying to get this to function, randomly chooses 1 of the 3 color choices. Is rand only capable of generating numbers? or Numbers/letters in a series.

<div onclick="location.href='<?php the_permalink() ?>';"
style="cursor:pointer;background:#<?php echo rand (DDCA73, A0AA47, ADBAE1)?>;" 
class="post bg thickbox" id="thickbox post-<?php the_ID(); ?>">

Upvotes: 1

Views: 213

Answers (1)

Codex73
Codex73

Reputation: 5766

** rand or mt_rand randomize numbers not strings. Create an array, then randomize it's key selection.

<?php
    function random_color(){                   
            $colors_avail = array("DDCA73","A0AA47","ADBAE1");      
            $colors_count = count($colors_avail)-1; 
            $color_wheel = $colors_avail[mt_rand(0,$colors_count)];        
            echo $color_wheel;
     }
?>

<div onclick="location.href='<?php the_permalink() ?>';"
style="cursor:pointer;background:#<?php random_color(); ?>;" 
class="post bg thickbox" id="thickbox post-<?php the_ID(); ?>">

Upvotes: 1

Related Questions