0

In PHP, I'm trying to add a different colored block for each different type of music genre. Each color represents a specific genre. I have 12 genres, but I've only included 3 of those for simplification purposes.

First I get the genre value with

$genre = get_post_meta($post->ID, 'genre', true);

Then I'm using an if statement like so (though it doesn't work past the 2nd genre)

if($genre == "EDM")
echo '<div style="display:block;width:100%;height:10px;background-color:#9B86FF;"></div>';

else if($genre == "Hip-Hop")
echo '<div style="display:block;width:100%;height:10px;background-color:#56FFCE;"></div>';

else if($genre == "Rock")
echo '<div style="display:block;width:100%;height:10px;background-color:#56CBFD;"></div>';

It works for EDM and Hip-Hop, but stops working after Hip-Hop.

Any help is greatly appreciated!

1
  • 4
    make your hip hop to string like "Hip-Hop" Commented Mar 2, 2016 at 6:35

1 Answer 1

2

What about this?

$genre_colors = array("EDM" => "9B86FF", "Hip-Hop" => "56FFCE", "Rock" => "56CBFD");
$color = $genre_colors[get_post_meta($post->ID, 'genre', true)];
if ($color !== null)
  echo '<div style="display:block;width:100%;height:10px;background-color:#' . $color . ';"></div>';
Sign up to request clarification or add additional context in comments.

2 Comments

Removed the hash from the EDM color hex value and this worked beautifully! Thank you so much!!
Good Answer :) You can also write your last statement as: echo "<div style='display:block;width:100%;height:10px;background-color:#E6E6FA';></div>";

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.