sage icon indicating copy to clipboard operation
sage copied to clipboard

Nested menu drop downs not working in Roots 5.0

Open aboutaaron opened this issue 14 years ago • 21 comments

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.

aboutaaron avatar Feb 06 '12 19:02 aboutaaron

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/

retlehs avatar Feb 06 '12 19:02 retlehs

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?

aboutaaron avatar Feb 06 '12 19:02 aboutaaron

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?

retlehs avatar Feb 06 '12 20:02 retlehs

You can make the sub menu show on hover instead of click, it'd require some tweeks in the bootstrap css files though

jahvi avatar Feb 11 '12 14:02 jahvi

@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

sonoo avatar Feb 16 '12 17:02 sonoo

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?

iagdotme avatar Feb 20 '12 12:02 iagdotme

@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 avatar Feb 21 '12 21:02 aboutaaron

@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?

iagdotme avatar Feb 21 '12 22:02 iagdotme

@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

retlehs avatar Feb 21 '12 22:02 retlehs

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.

iagdotme avatar Feb 21 '12 22:02 iagdotme

@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

aboutaaron avatar Feb 21 '12 23:02 aboutaaron

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 . ''; $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : ''; $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : ''; $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : ''; $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : ''; $attributes .= ($args->has_children) ? ' class="dropdown-toggle" data-toggle="dropdown"' : ''; $item_output = $args->before; $item_output .= ''; $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after; $item_output .= ($args->has_children) ? ' ' : ''; $item_output .= $args->after; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) { if ( !$element ) return; $id_field = $this->db_fields['id']; //display this element if ( is_array( $args[0] ) ) $args[0]['has_children'] = ! empty( $children_elements[$element->$id_field] ); else if ( is_object( $args[0] ) ) $args[0]->has_children = ! empty( $children_elements[$element->$id_field] ); $cb_args = array_merge( array(&$output, $element, $depth), $args); call_user_func_array(array(&$this, 'start_el'), $cb_args); $id = $element->$id_field; // descend only when the depth is right and there are childrens for this element if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) { foreach( $children_elements[ $id ] as $child ){ if ( !isset($newlevel) ) { $newlevel = true; //start the child delimiter $cb_args = array_merge( array(&$output, $depth), $args); call_user_func_array(array(&$this, 'start_lvl'), $cb_args); } $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output ); } unset( $children_elements[ $id ] ); } if ( isset($newlevel) && $newlevel ){ //end the child delimiter $cb_args = array_merge( array(&$output, $depth), $args); call_user_func_array(array(&$this, 'end_lvl'), $cb_args); } //end this element $cb_args = array_merge( array(&$output, $element, $depth), $args); call_user_func_array(array(&$this, 'end_el'), $cb_args); } } } ``` endif; ?>

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.

huwrowlands avatar Feb 23 '12 15:02 huwrowlands

http://www.markdotto.com/2012/02/27/bootstrap-explained-dropdowns/

retlehs avatar Feb 27 '12 15:02 retlehs

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; }

ChrisClayton avatar Feb 29 '12 22:02 ChrisClayton

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:

  1. How do you link through to the parent page now that it's an anchor link?
  2. 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.
  3. It's not very semantic. An blank anchor tag doesn't mean anything.
  4. 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.

iagdotme avatar Mar 02 '12 11:03 iagdotme

@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.

ChrisClayton avatar Mar 03 '12 07:03 ChrisClayton

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 + '#'; });
  }
);

wohlford avatar Mar 27 '12 01:03 wohlford

i have the same problem fixed with the code of wohlford inserted in main.js file. thanks wohlford!

Mte90 avatar May 31 '12 09:05 Mte90

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

fosion avatar Jun 22 '12 15:06 fosion

@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.

StevenCM avatar Oct 27 '12 13:10 StevenCM