I used a fix to enable multilevel dropdown menu but submenu is going out of screen
Hi, I am using navwalker and it is working great so far. I wanted a multilevel dropdown menu for which i tried the fix given at https://github.com/twittem/wp-bootstrap-navwalker/wiki/Fix-Multi-Levels-Bootstrap-Menu-and-Open-on-Mouse-Hover And it is working fine for me but the only issue i am facing is dropdown-submenu for the menu item at right corner of navbar goes out of screen. I saw that you are working on future version and many features so i believe you should have a look at this issue and please let me know if there is any possible solution..
Declare Nav with 'Depth'
wp_nav_menu(['theme_location' => 'primary_navigation','depth'=> 7,'walker' => new wp_bootstrap_navwalker(), 'menu_class' => 'nav navbar-nav']);
Change wp_bootstrap_navwalker Class
** Replace - if ( $args->has_children ) $class_names .= ' dropdown';
With - if($args->has_children && $depth === 0) { $class_names .= ' dropdown'; }
elseif($args->has_children && $depth > 0) {$class_names .= ' dropdown dropdown-submenu'; }
** Replace - if ( $args->has_children && $depth === 0 ) {
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['class'] = 'dropdown-toggle';
$atts['aria-haspopup'] = 'true';
}
With - if ( $args->has_children ) {
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['class'] = 'dropdown-toggle';
$atts['aria-haspopup'] = 'true';
}
** Replace - $item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
With - $item_output .= ( $args->has_children ) ? ' <span class="caret"></span></a>' : '</a>';
Add JQuery
(function($){
$(document).ready(function(){
$('ul.dropdown-menu [data-toggle=dropdown]').on('click', function(event) {
event.preventDefault();
event.stopPropagation();
$(this).parent().siblings().removeClass('open');
$(this).parent().toggleClass('open');
});
});
})(jQuery);
Add CSS
.dropdown-submenu {
position:relative;
>.dropdown-menu {
top:0;
left:100%;
margin-top:-6px;
margin-left:-1px;
}
}
Thanks a lot @gvanimurthy it really fixed the issue. You guys should put this in the wiki or something. very useful and important
also the css is
.dropdown-submenu {
position: relative;
}
.dropdown-submenu .dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
}
Multilevel drop down has been discussed dozens of times in the issues and is discussed in the readme.
The walker very purposely does not support multilevel drop downs. Bootstrap 3 doesn't natively support multilevel drop downs, and this walker is ment to implement Bootstrap 3, nothing more, nothing less. Not to mention as a general practice multilevel drop downs are bad UX.
hi, there
i have the same isue, can you tell me please in which file to put jquery function?
thank you in advance.
@emmy86 you can add jQuery in any js file you would be loading
hello,
thank you so much for the briefe response.
i havesaved the jquery code on an file and then loaded this file with functions.php file but my site comes shorter and could not display all the page. i have used this tecnique:
//this goes in functions.php near the top function my_scripts_method() { // register your script location, dependencies and version wp_register_script('custom_script', get_template_directory_uri() . '/custom_js/jquery_test.js', array('jquery'), '1.0' ); // enqueue the script wp_enqueue_script('custom_script'); } add_action('wp_enqueue_scripts', 'my_scripts_method');
than, i have tried with another teqnicue:
function construction_load_scripts(){
// Register or enqueue scripts
wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap', get_template_directory_uri(). '/js/bootstrap.min.js', array('jquery'), '3.0.1' );
wp_enqueue_script('dropdown', get_template_directory_uri(). '/js/jquery.dropdown.js', array('jquery'), '3.0.1' );
wp_enqueue_script('scripts', get_template_directory_uri() .'/js/construction.js', array('jquery'), '3.2', true);
wp_register_script('Google-map-api', 'http://maps.google.com/maps/api/js?sensor=false', array('jquery'), '3.2', false);
wp_register_script('Google-map', get_template_directory_uri().'/js/gmap3.min.js', array('jquery'), '3.2', false);
if (is_page_template('under-construction.php'))
{
wp_enqueue_script('Under-construction', get_template_directory_uri().'/js/jquery.countdown.js', array('jquery'), '3.2', true);
}
}
and it cames an blank page :(
what have i done wrong?
thank you so much!
Thanks @gvanimurthy, your solution worked fine for me.
@emmy86 About the jQuery, this is what works for me:
function loading_custom_scripts() { // register script for sub-menu support wp_register_script('custom_script',get_template_directory_uri() . '/js/scripts.js',array('jquery'),'1.0' ); // enqueue the script wp_enqueue_script('custom_script'); }
Has anyone tried on tablet?
On mine, dropdown is working fine. When clicking (touching) the sub-menu, it doesnt open further.