Using variables as html class
-
Hey,
Just to start off, I’m a noob at php coding, and am still trying to understand the basics of it. I’m designing my own theme and am having trouble figuring out how to perform a URL check and then change a variable’s value depending on what the URL return is. I have a three-image sprite navigation bar that links to my pages. I gave each link a class that I want to put a variable in, so if the url = sitename.com/contact, i want the contact link to have the class called “current”, and in my css I have the current state set up for the correct image. This worked in html where on each page I just set that particular link button to the “current” class, but being a wordpress site, I have to do it dynamically within the header.php.
The nav bar looks like this so far with the code I’m trying to use (only written for the homepage and work page as of now):
<div id="wrapper" class="hfeed"> <?php $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?> <?php $homeURL = ( $url == bloginfo('url') ) ? 'current' : 'null'; ?> <?php $workURL = ( $url == bloginfo('url') /work) ? 'current' : 'null'; ?> <div id="navbar"> <ul id="navigation"> <li id="navigation-1"><a href="<?php bloginfo('url') ?>/" title="home" class="<?php $homeURL ?>"><span>home</span></a></li> <li id="navigation-2"><a href="<?php bloginfo('url') ?>/work" title="work" class="<?php $workURL ?>"><span>work</span></a></li> <li id="navigation-3"><a href="<?php bloginfo('url') ?>/bio" title="bio"><span>bio</span></a></li> <li id="navigation-4"><a href="<?php bloginfo('url') ?>/contact" title="contact"><span>contact</span></a></li> </ul> </div><!-- navbar -->Its getting the URL just fine, but its printing it on the page some reason, which I don’t understand since there’s no print or echo. And its also not setting the class to “current” because my links are just in their non-active state. Any help would be greatly appreciated. I’m not sure if I’m even taking the proper approach with this, so please point me in the right direction, thanks!
The topic ‘Using variables as html class’ is closed to new replies.