Dev Notes

Various Cheat Sheets and Resources by David Egan/Carawebs.

Bootstrap 3 Header Markup for Sage WordPress Theme

Bootstrap, Sage, WordPress
David Egan

Sage is an excellent starter theme for WordPress. It comes with Bootstrap connected out-of-the-box, but it does not have a “standard” Bootstrap nav markup. The old Roots theme (Sage’s parent) used to have a NavWalker() class that extended the WordPress Walker_Nav_Menu() class to give cleaner menu markup and Bootstrap friendly classes.

To add the NavWalker() class, from your theme root, run:

wget -O lib/nav.php

NB: see update below for a potentially better option.

Then replace the code in your templates/header.php with this (or similar Bootstrap-style markup):

<?php require_once( get_template_directory() . '/lib/nav.php' ); ?>
<header class="banner navbar navbar-inverse navbar-fixed-top navbar-shrink" role="banner">
  <div class="container">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target=".navbar-collapse">
        <span class="sr-only"><?= __('Toggle navigation', 'sage'); ?></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      <a class="navbar-brand" href="<?= esc_url(home_url('/')); ?>"><?php bloginfo('name'); ?></a>
    <nav class="collapse navbar-collapse" role="navigation">
      if (has_nav_menu('primary_navigation')) :

        wp_nav_menu(['theme_location' => 'primary_navigation', 'walker' => new Roots\Sage\Nav\NavWalker(), 'menu_class' => 'nav navbar-nav']);



If you are using the navbar-fixed-top class on the navbar element, you’ll need to add the following style rules:

// Offset navbar from top if logged in to allow for the WP admin toolbar
// -----------------------------------------------------------------------------
.logged-in .navbar-fixed-top {
  margin-top: 30px;
  // At the extra small screen and smaller, WP toolbar is taller
  @media (max-width: $screen-xs-max) {
  	margin-top: 46px;
// If the navbar is fixed, push down the wrap
// -----------------------------------------------------------------------------
.navbar-fixed-top + .wrap {
  margin-top: 78px;

Update September 2016

The original article didn’t provide the license for the NavWalker. I’ve now included the license in the file docblock.

The NavWalker above is from quite an old version of Roots - I can’t remember the exact version. The Roots NavWalker class was dropped in March 2015. The link below is for the most recent version, and it may be better for your purposes:

I haven’t tried this class, but I will on my next WP project. It looks like it handles custom post types in a better way than the older NavWalker referenced in my original article.

To copy it into your project, run:

wget -O lib/nav.php

comments powered by Disqus