Title: Using variables as html class
Last modified: August 19, 2016

---

# Using variables as html class

 *  Resolved [codycuellar](https://wordpress.org/support/users/codycuellar/)
 * (@codycuellar)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/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!

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/using-variables-as-html-class/#post-1546551)
 * instead of this, for instance:
    `$url == bloginfo('url')` use `$url == get_bloginfo('
   url')`
 * [http://codex.wordpress.org/Function_Reference/get_bloginfo](http://codex.wordpress.org/Function_Reference/get_bloginfo)
 * `bloginfo()` echos the result immmedeately; while `get_bloginfo()` returns the
   result as a string, either to be used in a variable, or to be used in comparisons.
 *  Thread Starter [codycuellar](https://wordpress.org/support/users/codycuellar/)
 * (@codycuellar)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/using-variables-as-html-class/#post-1546571)
 * Excellent! Thank you, it works perfectly now after a bit of tweaking. However
   one more question regarding this topic (also sparked from one issue I was having).
   As I was testing the variables I just used echo to display the variables on screen
   so I could see what they were returning and for the longest time my $url and 
   get_bloginfo(‘url’) appeared to be returning the exact same value, until I realized
   that the get_bloginfo didn’t put a / at the end, so my $homeURL kept returning“
   null”. I had to change it to get_bloginfo(‘url’).”/” so my $homeURL would return
   the result “current”.
 * Now my question is, is there a way that I can make the $url check if it begins
   with a certain string and disregard anything that comes after it? For instance,
   if my custom permalinks start with [http://www.site.com/blog](http://www.site.com/blog)
   for every post, I want the blog link in my nav bar to stay activated. When the
   function runs to assign the url to $url, it will read [http://www.site.com/blog/category/year/month/post-title](http://www.site.com/blog/category/year/month/post-title),
   but since it begins with [http://www.site.com/blog](http://www.site.com/blog)
   I want $homeURL to still return true and give me the “current” value – and disregard
   whatever comes after such as the category, year, etc. So rather than == is there
   a “begins with” type of operator or is this going to be much more involved?
 * Anyway here’s the final working code:
 *     ```
       <?php $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>
       		  <?php
                     $homeURL = ( $url == get_bloginfo('url').'/' ) ? 'current' : 'null';
       			  $workURL = ( $url == get_bloginfo('url').'/work' ) ? 'current' : 'null';
       			  $bioURL = ( $url == get_bloginfo('url').'/bio' ) ? 'current' : 'null';
       			  $contactURL = ( $url == get_bloginfo('url').'/contact' ) ? 'current' : 'null';
       		  ?>
             <div id="navbar">
               <ul id="navigation">
   
                 <li id="navigation-1"><a href="<?php bloginfo('url') ?>/" title="home" class="<?php echo "$homeURL" ?>"><span>home</span></a></li>
                 <li id="navigation-2"><a href="<?php bloginfo('url') ?>/work" title="work" class="<?php echo "$workURL" ?>"><span>work</span></a></li>
                 <li id="navigation-3"><a href="<?php bloginfo('url') ?>/bio" title="bio" class="<?php echo "$bioURL" ?>"><span>bio</span></a></li>
                 <li id="navigation-4"><a href="<?php bloginfo('url') ?>/contact" title="contact" class="<?php echo "$contactURL" ?>"><span>contact</span></a></li>
               </ul>
             </div><!-- navbar -->
       ```
   
 *  Thread Starter [codycuellar](https://wordpress.org/support/users/codycuellar/)
 * (@codycuellar)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/using-variables-as-html-class/#post-1546572)
 * I tried using >= and thought it solved it, but then I realized it was giving 
   me really weird results and not actually doing what I thought it would do.
 *  Thread Starter [codycuellar](https://wordpress.org/support/users/codycuellar/)
 * (@codycuellar)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/using-variables-as-html-class/#post-1546583)
 * Oh the power of Google. Solved it finally using strpos(); function.
 * Here’s the new working code:
 *     ```
       <?php
       			  $homeURL = ( $url == get_bloginfo('url').'/' ) ? 'current' : 'null';
   
       			  $workcheck = strpos($url, get_bloginfo('url').'/work');
       			  $workURL = ($workcheck === false) ? "null" : "current";
   
       			  $biocheck = strpos($url, get_bloginfo('url').'/bio');
       			  $bioURL = ($biocheck === false) ? "null" : "current";
   
       			  $contactcheck = strpos($url, get_bloginfo('url').'/contact');
       			  $contactURL = ($contactcheck === false) ? "null" : "current";
       		  ?>
       ```
   
 * In the link class field I kept it as:
    `<?php echo "$homeURL" ?>`
 *  [izvarinskij](https://wordpress.org/support/users/izvarinskij/)
 * (@izvarinskij)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/using-variables-as-html-class/#post-1546624)
 * By the way, you can use `href="/"`, `href="/work"`, `href="/bio"`, `href="/contact"`
   without `<?php bloginfo('url') ?>`, it works for me (it will generate relative
   links, instead of absolute).

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Using variables as html class’ is closed to new replies.

## Tags

 * [class](https://wordpress.org/support/topic-tag/class/)
 * [get](https://wordpress.org/support/topic-tag/get/)
 * [url](https://wordpress.org/support/topic-tag/url/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * 5 replies
 * 3 participants
 * Last reply from: [izvarinskij](https://wordpress.org/support/users/izvarinskij/)
 * Last activity: [15 years, 11 months ago](https://wordpress.org/support/topic/using-variables-as-html-class/#post-1546624)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
