These days I recommend using the Genesis Framework for WordPress and in the sites I build I incorporate the new Bootstrap v4 grid system into that.
10. The complicated bit – the menu and Bootstrap walker
Converting menus from a flat HTML design into a fully functional WordPress menu can often be quite tricky if you’re trying to reproduce the design exactly. You’ll notice that most Bootstrap HTML templates you buy from the Theme shops don’t use the default Bootstrap navbar – because it does look a bit plain and boring. Especially the Bootstrap 2 version.
Most themes use the popular Superfish menu javascript plugin. It’s easy to get the .js and .css files for it enqueued like the others mentioned earlier, but you need to get WordPress to output its menus with all the css classes that the Superfish styling and jQuery is based upon.
Another important consideration is what you’re going to do with dropdown menus when your site is viewed on a mobile/tablet that doesn’t have a mouse hover function to reveal the menu. The standard Bootstrap functionality is that you have to click on the link in order for the dropdown sub-menu to appear. That may be considered good practice, but you hardly ever see it out ‘in the wild’ on many websites. Far more commonly you see menus where you hover on an item to reveal its children, OR click on it to visit its hyperlink. That’s no good for our nice responsive mobile phone menu though. You can’t hover on an item, you can only click it – so is that going to open the submenu, or just act as a regular hyperlink?
I don’t think that there’s yet a really intuitive WordPress solution to all this. The best bet at the moment is to make your parent menu item’s link ’empty’ i.e. just put a ‘#’ instead of a url. Now it only has one function – which will be to open/close its child sub-menu. This means you might have to end up with a parent menu item of ‘About Us’, and then 3 or 4 sub-menu items – one of which might also be ‘About Us’ if you wanted an About Us page.
So for example you would have to set up your WordPress menu like this
-Home
-About Us – link to ‘#’
— About Us – link to ‘/about-us’ page
— Our Staff – link to ‘/about-us/staff’ page
— Contact Us – link to ‘/about-us/’ page
-What We Do – link to ‘/what-we-do’ page
-Donate – link to ‘/donate’ page
Of course this is all much easier if your menus aren’t going to have any dropdown sub-menus in them. That’s certainly a direction that a trend in web design is moving towards because of the fact that dropdown menus don’t work all that great for responsive design. In order to future-proof your site though, it’s a good idea to plan for how dropdowns would work just in case you need to add one. This is especially true if you are setting up a CMS system for a client to use – as it’s hard to prevent them from setting up sub-menus from within the Dashboard>Appearance>Menus screen.
What is a WordPress Walker?
When it comes ot setting up menus in WordPress, you’ll come across many mentions of ‘walkers’ – these are essentially PHP functions that take the standard WordPress menu output of divs, classes, ul, li etc – and rewrite them so they are formatted in a way that your site’s .js and .css files expect them.
Bootstrap in particular has a very specific way that its navbars have to be laid out, and css classes that they need to have. If your menu doesn’t follow the same structure then the Bootstrap files won’t know what to do with it and it won’t work.
Superfish menus play nicer with WordPress ones out of the box, and don’t necessarily need their own ‘Walker’ file. Let’s have a look at the two of them, even though we aren’t actually using the native Bootstrap menu on this site we’re building.
I’m going to assume that you know how to create a menu in WordPress, and have assigned your main navigation menu as the ‘Primary’ one.
You should also add this code to your functions.php in order to enable your theme to support WordPress menus. It seems a bit silly that a theme wouldn’t do this by default, but if your menus aren’t appearing on your site then make sure you’ve got this code included:
function register_my_menus() { register_nav_menus( array( 'primary' => __( 'Primary' ), 'secondary' => __( 'Secondary' ) ) ); } add_action( 'init', 'register_my_menus' );
1. The native Bootstrap menu solution
As mentioned earlier, although Bootstrap is a hugely popular framework, it’s often its menus that are the bits that designers tend to disregard for something that looks nicer. Having said that though, the Bootstrap ones are great for mobile/tablet screen sizes and they have already figured out a solution for how to deal with those pesky dropdown menus.
Writing your own ‘Walker’ file is a very complicated business, but as with most things in WordPress/Bootstrap – someone has already done it and you can happily copy theirs.
Here’s a version that works well with Bootstrap v2. If you are using Bootstrap v3 then this may need a bit of tweaking, or you can just do a Google search for ‘Bootstrap 3 WordPress walker’.
Paste this code into your functions.php file:
/*** Add Bootstrap dropdown-menu class to submenu items **/ /**** See https://gist.github.com/johnmegahan/1597994 and http://goodandorgreat.wordpress.com/2012/01/12/update-2-using-twitter-bootstrap-dropdown-menus-with-wordpress/ */ add_action( 'after_setup_theme', 'bootstrap_setup' ); if ( ! function_exists( 'bootstrap_setup' ) ): function bootstrap_setup(){ add_action( 'init', 'register_menu' ); function register_menu(){ register_nav_menu( 'top-bar', 'Bootstrap Top Menu' ); } class Bootstrap_Walker_Nav_Menu extends Walker_Nav_Menu { function start_lvl( &$output, $depth ) { $indent = str_repeat( "\t", $depth ); $output .= "\n$indent