Skip to content Skip to sidebar Skip to footer

Wp Add Logo Into Center Of Menu Whist Ignoring Sub Menus?

Using the following jQuery form another post I'm able to dynamically center a logo in the middle of a wordpress menu but when I add a sub menu to the main nav, the logo dissapears.

Solution 1:

var position = $("ul#main_nav > li").length-2;
                    var i = 0;
                    $('ul#main_nav li').each(function() {
                        if(i == position/2) {
                        $(this).after('<imgsrc="<?phpecho get_template_directory_uri(); ?>/img/logo.png"alt="Logo"class="logo-img">');
                        }
                        i++;
                    });
    /* Try this */

Solution 2:

If by any chance anyone else comes across this issue, here's the fix..

var position = $("ul#main_nav li.menu-item-object-page").length-2;
var i = 0;
    $('ul#main_nav li.menu-item-object-page).each(function() {
        if(i == position/2) {
        $(this).after('<imgsrc="<?phpecho get_template_directory_uri(); ?>/img/logo.png"alt="Logo"class="logo-img">');
    }
 i++;
});

Wordpress gives top level nav list items the class .menu-item-object-page but doesn't apply this to sub menu items so by being a bit more specific about what elemnets to target I've got the result I wanted.

Post a Comment for "Wp Add Logo Into Center Of Menu Whist Ignoring Sub Menus?"