I have a custom field that contains a list of comma separated colors.
I want to randomly select one of those colors.
If get_post_meta returns an array I thought I could randomly pick from that array.
If I try to echo $rand_color_value it just gives me the whole array
How can I randomly select one color from the list in the custom field.
$agrs = array(
'post_type' => 'page',
'tag' => 'colors'
);
$colorLoop = new WP_Query($agrs);
if($colorLoop->have_posts()):
while($colorLoop->have_posts()):
$colorLoop->the_post();
$theColor = get_post_meta($post->ID, 'colors', false);
/*random color*/
$rand_color = array_rand($theColor,1);
$rand_color_value = $theColor[$rand_color];
echo $rand_color_value;
?>
<?php endwhile; endif;?>
<?php wp_reset_postdata; ?>
string
of comma separated values? Or do you actually store anarray
in the option? Also what exactly do the variables$theColor
, and$theWeight
contain? Tryvar_dump(compact('theColor', 'theWeight'));
string
but you're talking about the option containing anarray
? If it's astring
then I can tell you what the problem probably is, if it's anarray
then you'll need to do some additional debugging.