Nested menu drop downs not working in Roots 5.0
Hey,
I'm beginning development on a site and when I changed the theme use from 4.0 to 5.0, I noticed menu dropdown functionality stopped.
dev site is located at aboutaaron.com/dev
The dropdown indicator appears in the menu, but clicking it doesn't open the nested menu.
When I checked the console for errors I saw this:
Syntax error, unrecognized expression: /dev/sample-page/ (<-- this is name of the page link)
f.event={add:function(a,c,d,e,g){var h...hild(d);return this},empty:function()
http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js Line 3
I'm not entirely sure why this is an issue, but any help into the matter would be great.
the bootstrap docs show the anchor for the top level of a dropdown as #
currently how i'm doing it over on http://demo.rootstheme.com/
Thanks for the speedy reply Ben.
And I see what you're saying.
So you cannot have the menu item clickable with content. It has to act as a dropdown only. My client wants to have the items in the navbar link to content. Would there be a way around that?
i'd switch away from using the topbar navigation and create your own - remove the call to the topbar walker on the primary nav and it will use the regular roots clean walker instead
have you looked through bootstrap issues to see if anyone else has discussed this?
You can make the sub menu show on hover instead of click, it'd require some tweeks in the bootstrap css files though
@javiervd - do you have the tweaks handy to make the sub menu show on hover instead of click. Much Appreciate if you can share your tweaks
Just upgraded to latest Roots and taken the plunge with Bootstrap. I'm getting the same problem with sub navigation. It worked in previous Roots, but not anymore. I'm getting the same issue as @aboutaaron in that it just takes you to the parent page and doesn't show the dropdown. My Javascript console shows a similar error too. Any ideas on how to fix this? I'd have thought this should work straight out of the box? Also, is there a way for the dropdown to show on hover, or is the default to show only when clicking the downwards arrow?
@baritoneuk the parent menu element needs to have an href of ''#". So, in the WordPress admin, under Menus, the Primary Navigation Parent page would actually me a Custom Link with the value of #. The child elements underneath will then pop up when the parent element is clicked. The markup would more less look like this:
<ul>
<li>
<!-- Set in WordPress and triggers dropdown:
Dashboard > Appearance > Menus > Custom Link > Link Value = # -->
<a class="dropdown-toggele" data-toggle="dropdown" href="#">My Parent Element</a>
<ul class="dropdown-menu">
<li>
<!-- The actual page entry that the user will clink -->
<a href="/my_page.html">Child 1</a>
</li>
<li>
<!-- The actual page entry that the user will clink -->
<a href="my_other_page.html">Child 2</a>
</li>
</ul>
</li>
</ul>
As far as to trigger the dropdown on hover, I'm sure that's capable in Bootstrap, but I haven't personally tried to do it.
Based on the documentation from Bootstrap, you could probably use the jQuery .hover() method or something. http://twitter.github.com/bootstrap/javascript.html#tabs
@aboutaaron That's not the way Roots worked in the past. I'd have thought you'd have the parent element as the parent page and each sub page as the dropdown. How else will you be able to get to the parent page? I'm really wanting to have the dropdown to appear on hover over. Isn't that how it's supposed to work out of the box?
@baritoneuk if you don't want to use the topbar like it's meant to be used in bootstrap then just don't use the navbar nav walker.
edit header.php to remove the navbar classes and remove the call to the custom walker
Thanks, @retlehs Do you know why Bootstrap is implemented like this? I'm sure a lot of thought went into this way of doing it, but it's not making sense to me. Where should the parent page go in the list? Should it be at the top of the dropdown menu? I don't think using # is semantic or good for accessibility.
@baritoneuk That was the "out-the-box" functionality with Roots, but per Bootstrap's style, it was removed. As @retlehs said, you can also just remove the Bootstrap Nav class (.nav-collapse) from header.php , remove the walker and then roll your own:
<nav id="nav-main" class="nav-collapse" role="navigation">
<?php wp_nav_menu(array('theme_location' => 'primary_navigation', 'walker' => new Roots_Navbar_Nav_Walker())); ?>
</nav>
to:
<nav>
<?php wp_nav_menu(array('theme_location' => 'primary_navigation')); ?>
</nav>
As far as your comment about why Bootstrap choose this menu choice check https://github.com/twitter/bootstrap/issues/424 and https://github.com/twitter/bootstrap/issues/983.
If you want mouseover dropdowns, check http://stackoverflow.com/questions/8878033/how-to-make-twitter-bootstrap-menu-dropdown-on-hover-rather-than-click/8878666#8878666
Found a solution to this which worked for me, here is the guy who solved it: http://goodandorgreat.wordpress.com/2012/01/12/update-2-using-twitter-bootstrap-dropdown-menus-with-wordpress/ I have not tried this using Roots yet....
EDIT: I have tried it but could not get it to function correctly, but perhaps someone here with more knowledge could?
Add this to your functions.php file:
\n"; } function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { $indent = ( $depth ) ? str_repeat( "\t", $depth ) : ''; $li_attributes = ''; $class_names = $value = ''; $classes = empty( $item->classes ) ? array() : (array) $item->classes; if ($args->has_children){ $classes[] = 'dropdown'; } $classes[] = 'menu-item-' . $item->ID; $classes[] = ($item->current) ? 'active' : ''; $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) ); $class_names = ' class="' . esc_attr( $class_names ) . '"'; $id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args ); $id = strlen( $id ) ? ' id="' . esc_attr( $id ) . '"' : ''; $output .= $indent . 'Then add this to the header.php file:
<nav id="nav-main" class="nav-collapse" role="navigation">
<?php
$args = array(
'theme_location' => 'top-bar',
'depth' => 2,
'walker' => new Bootstrap_Walker_Nav_Menu()
);
wp_nav_menu($args);
?>
<?php //wp_nav_menu(array('theme_location' => 'primary_navigation', 'walker' => new Roots_Navbar_Nav_Walker())); ?>
</nav>
Hope this does solve the issue.
http://www.markdotto.com/2012/02/27/bootstrap-explained-dropdowns/
As mentioned in @retlehs link, it was a delibrate design decision by the developers of bootstrap. If anyone wants to change it so that the parent link is an actual link AND for it to dropdown on hover, you can use CSS
ul.nav li.dropdown:hover ul.dropdown-menu{ display: block; }
As I was the person who posted the question on Stackoverflow that Mark Dotto referred to, I thought I'd chip in here.
I can see a major advantage in the Bootstrap implementation of dropdowns- and that is that they work on mobile devices. Showing a dropdown on hover is very difficult on a mobile- how do you "hover"?!
However there are still big issues in my (very) humble opinion with the implementation they have adopted. I emailed Mark about this, although I've yet to hear a response. Perhaps I am being overly pedantic or missing something. If so, I hope to be corrected!
It's usual for a navigation menu to mirror the site architecture. The top level pages being the main links, and 2nd level pages being in a dropdown menu under each top link. It's also usual to use lists and sub lists for this:
<ul>
<li><a href="/" title="Home Page (level 1)">Home</a></li>
<li><a href="/about/" title="About Page (level 1)">About</a>
<ul>
<li><a href="/about/the-team/" title="The Team / About (level 2)">The Team</a></li>
<li><a href="/about/others/" title="Others / About (level 2)">Others</a></li>
</ul>
</li>
<li><a href="/contact/" title="Contact Page (level 1)">Contact</a></li>
</ul>
Here you can click the top level pages, and traditionally you'd see the dropdown appear when you hovered over it.
The issue I have with the implementation in Bootstrap is that you have to set the top page as an anchor tag. @retlehs refers to the Bootstrap docs above by saying you have to show the anchor for the top level of a dropdown as "#" This has a number of issues in my mind:
- How do you link through to the parent page now that it's an anchor link?
- You could put the parent link in the sub list above the sub pages. But this isn't mirroring the architecture of the site. In my view this also makes it less accessible.
- It's not very semantic. An blank anchor tag doesn't mean anything.
- It relies on javascript. OK, most people have Javascript on, but I'm a great believer in graceful degregation. It should work as expected with Javascript switched off, but you can do even more cool things with it switched on.
This is more of a Bootstrap issue, but it does affect Roots. It makes creating menus in Wordpress much more complicated. It will be difficult to explain to clients that they need to create a dummy top level element with a "#" for each dropdown.
@baritoneuk I'm on a mixed-thoughts on the "implementation they have adopted". While i do agree with you - we need to remember that bootstrap was initially created as a style guide to 'show off' and document all the styles they use on twitter.
i personally think that Roots SHOULD eventually modify some of bootstrap's functionality to better suit WordPress functionality. I don't think it needs to be done right now, though.
I'm challenged to accept this issue as closed. It feels very much like a bug to me. Whether or not this is the way Bootstrap works, it does not fit very well into the Wordpress model for menus.
Here is my contribution to the issue:
$(document).ready(
function () {
$('nav > ul > li:has(ul) > a').attr('href', function (i, val) { return val + '#'; });
}
);
i have the same problem fixed with the code of wohlford inserted in main.js file. thanks wohlford!
BTW: Wordpress has a Menu System (Design->Menu), just create a custom link '#' and add the content below this link... No jQuery Magic needed ...
have fun
@wohlford @retlehs
I've build a new Wordpress site WP v3.4.2 & Roothstheme v6.1.0 Seems like this issue is still pending? No parent link for the dropdown menu? My solution here was to remove the data attribute data-toggle="dropdown"
$(document).ready(function() {
$('nav > ul > li:has(ul) > a').attr('href', function (i, val) { this.removeAttribute("data-toggle"); });
}
)
This solution is based on wohlford's code. Not sure if this is the best way to write this piece of code but it works for me. The parent links are working/clickable again.