0

I'm using the bootstrap theme, by default in page.tpl.php the nav bar is outputted like so:

<header id="navbar" role="banner" class="<?php print $navbar_classes; ?>">

Where can I edit the $navbar_classes variable called in the header tag so I can add or remove classes?

  • for what purpose do you want to add new classes? – WaQaR Ali Jul 31 '15 at 10:15
  • I want the nav bar to be full width, which by default it isn't. – Jane Jul 31 '15 at 10:16
  • You will have to use the hierarchy of classes start from body tag, to access it an d use '!important' in in css style. – WaQaR Ali Jul 31 '15 at 10:29
  • you can also read that link – WaQaR Ali Jul 31 '15 at 10:52
3
function YOURTHEME_preprocess_page(&$vars) {
      //add news class
      $vars['navbar_classes_array'][] = 'new-class';
      //remove class
      unset($vars['navbar_classes_array'][0]);
      // you need change value 0 to value you want remove class
      // Default
      // 0  'navbar', 1 => 'container', 2 => 'navbar-default'

}

You can do it with

  1. Override file page.tpl.php. You can copy file page.tpl.php in bootstrap/theme/system/page.tpl.php to new your subtheme and edit it. It is right way.
  2. Or you can use function hook_preprocess_page(). You can write code in file template.php of your subtheme. That way you don't need change file page.tpl.php
0
  1. Open template.php file of your current active theme and apply this code;

    <?php        
       function phptemplate_preprocess_page(&$vars) {
         $vars['nav_menu'] = str_replace('class="navbar-nav"', 'class="myclass"',           
         menu_tree('navigation'));
       }
    ?>
    
  2. Now Open page.tpl.php file and you can print $nav_menu variable to add extra class in <header> tag.
0

To improve on MrD's very good answer, I added a little extra to make the code a little less breakable.

In your template.php file for your subtheme, create the following hook.

function YOURTHEME_preprocess_page(&$vars) {
    //adds container-fluid class 
    $vars['navbar_classes_array'][] = 'container-fluid';
    //removes class based on array value ('container' in this case)
       if(($key = array_search('container', $vars['navbar_classes_array']) !== false)) {
         unset($vars['navbar_classes_array'][$key]);
       }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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