"JQUERY NAVBAR CUSTOM ON WORDPRESS"
Bootstrap 4.1.1 Snippet by iammohitverma

<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <!------ Include the above in your HEAD tag ----------> <div class="container"> <div class="row"> <h2>Create your snippet's HTML, CSS and Javascript in the editor tabs</h2> </div> </div>
jQuery(".toggle-button").click( function (){ $(this).parent().toggleClass("menu-collapsed"); }); jQuery(document).ready(function() { jQuery("span.icon").click(function (){ jQuery(this).parent().toggleClass("menu-collapsed"); }); }); Create the function So I created a new custom walker function in functions.php and copied all the code of the core wp nav walker function (wp-includes/class-walker-nav-menu.php). Check for a menu item that has children I then added this array_search in public function start_el to check if the parent element has the class "menu-item-has-children": $has_children = array_search ( 'menu-item-has-children' , $classes ); Output the toggle button I then output the collapse element at the end of the start_el function if $has_children is true: if($has_children != false) : $item_output .= '<div class="toggle-button"></div>'; endif; Making it all work I then added jQuery to toggle a class on the parent element of the toggle button. jQuery(".toggle-button").click( function (){ $(this).parent().toggleClass("menu-collapsed"); }); This works perfectly, but is there a better way to check if the menu item has children? Searching for a class name just doesn't feel like the best solution. jQuery(document).ready(function() { jQuery("span.icon").click(function (){ jQuery(this).parent().toggleClass("menu-collapsed"); if(jQuery(this).parent().hasClass('menu-collapsed')) { jQuery(this).parent().find('ul.sub-menu').first().css("visibility", "visible"); } else { jQuery(this).parent().find('ul.sub-menu').first().css("visibility", "hidden"); } }); }); jQuery(document).ready(function() { jQuery("span.icon").click(function (){ jQuery(this).parent().toggleClass("menu-collapsed"); if(jQuery(this).parent().hasClass('menu-collapsed')) { jQuery(this).parent().find('ul.sub-menu').first().css("visibility", "visible"); } else { jQuery(this).parent().find('ul.sub-menu').first().css("visibility", "hidden"); } }); }); JQUERY NAVBAR CUSTOM ON WORDPRESS

Related: See More


Questions / Comments: