For various reasons, I can not use the Drupal 7 menu system and have to hardcode a special menu. I am trying to add an active class basing on the URL. I was looking at this page and trying the following.

        <ul class="nav navbar-nav">
            <li><span class="icon-search"></span><a href="/search" title="" class="<?php if ( ($path == $_GET['q'] || ($path == '/search') ) ) : print ('active');endif;?>">Search</a></li>
            <li><span class="icon-sports"><a href="/sports" title="">sports</a></li>
            <li><span class="icon-news"><a href="/news/" title="">news</a></li>
            <li><span class="icon-ent"><a href="/entertainment" title="">entertainment</a></li>
  • As side note, $base_url cannot be equal to $base_url .'/search', so the last snippet will surely fails.
    – apaderno
    Commented May 7, 2014 at 17:04

3 Answers 3


You can read URL with out base path using $_GET['q'] ..

<?php if ( $_GET['q'] == "search" ) : print ('active'); endif; ?>


You can use current_path API function which does the same...


Update, here is the working formula. Thanks Anil for pointing me in correct direction. I got this off this comment

$path = current_path();
$path_alias = drupal_get_path_alias(current_path());
<?php if ( $path_alias == "search" ) : print ('active'); endif;  ?>

this code is an exemple on how to add it to a div parent ( )

function yourtheme_preprocess_html(&$vars) {   
  * add contextual classes for each main menu sections for styling     purposes  *
 $url_components = explode('/', request_uri());
 if ('en'===$url_components[1])
    $vars['classes_array'][] = 'menu-en-section-'.$url_components[2];
    vars['classes_array'][] = 'menu-section-'.$url_components[2];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.