lundi 5 janvier 2015

The alternative solution to lots of ifelse statements in php

In my wordpress site, I've made it so a particular (fontawesome) icon will show if a custom field matches a particular string. Here's the relevant code:



<?php $department = get_field('department'); ?>

<?php
if($department == "Marketing"){ echo '<i class="fa fa-paint-brush fw fa-lg"></i> ';
} elseif($department == "Client Delivery"){echo '<i class="fa fa-headphones fa-lg"></i> ';
} elseif($department == "Implementation"){echo '<i class="fa fa-gears fw fa-lg"></i> ';
} elseif($department == "Sales"){echo '<i class="fa fa-money fw fa-lg"></i> ';
} elseif($department == "Executive"){echo '<i class="fa fa-building-o fw fa-lg"></i> ';
} elseif($department == "IT"){echo '<i class="fa fa-laptop fw fa-lg"></i> ';
}
?>


Now this works perfectly, but I just know there is a better way to achieve this, and I believe it's using a loop and/or an associative array. I've looked into both but I'm afraid I can't get my head around it, mainly because what I want to echo, is usually dependant on only one conditional.


Aucun commentaire:

Enregistrer un commentaire