I have added a li class to my wp_nav_menu with the following simple code
$('#menu-navigasjon').children('li').addClass('liparent');
When you visit my site http://annalidesign.com, WordPress will first generate the original navigation menu (vertical) without liparent class, and a split millisecond after that the Jquery code will run and add on the “liparent” class making it horizontal as I want.
The problem is that I can see how the navigation looks a split second before the new liparent class is added. Is there anything I can do to avoid this happening? I’ve tried to paste the Jquery script right after the HTML for the navbar, but the original vertical navigation bar still shows a split millisecond before.
I’ve tried using walker function, using the following code without any luck, because I don’t know how to ADD classes to the existing li element that WordPress generates. My code removes all the li classes WordPress generates and only adds one class. That is not what I want.
class MyWalker extends Walker_Nav_Menu {
function start_lvl(&$output, $depth) {
$indent = str_repeat("t", $depth);
$output .= "n$indent<ul class="sub-menu">n";
}
function start_el(&$output, $item, $depth, $args) {
if($depth == 0) {
$class_names = ' class="liparent"';
}
$output .= $indent . '<li' . $class_names . '>';
}
}
You could hide the menu initially then use jQuery to show it once the addClass function has run. Here’s a stripped-down test case that uses setTimeout to simulate a page loading before jQuery runs Codepen: http://codepen.io/SteveClason/pen/eZqBrw):
HTML
CSS
JavaScript