I’m using an if/else control structure to add class attributes to tags. Could I use a switch control structure instead and if so, how? If not, is there a better way to do this than what I’m doing?
<div <?php if (is_page( 'project' )) { echo 'class="project"'; }
elseif (is_page('home')) {echo 'class="home"'; }
elseif (is_page('contact')) {echo 'class="contact"'; }
?>>
</div>
While Ben’s answer is basically correct, is_page also checks to make sure that the query is indeed a Page and not something else with the same post_name.
So to be 100% accurate, you could do something like this:
Ok, now we have something to work with.
A switch is a way for saying “pick one of these choices based on this variables value” but an if/else statement is just a series of boolean checks. So, in your instance, a switch is the clear winner.