Wordpress Responsive Select Menu

2019-09-06 20:30发布

问题:

I have created a responsive site in WordPress. This site has number of different navigation areas which I am wanting to consolidate into to one select menu when site is viewed via mobile device.

The code in my WordPress header.php file currently looks like this:

<?php dropdown_menu( array('dropdown_title' => '-- Main Menu --', 'container' => 'div',  'theme_location'=>'main_menu') ); ?>

However, I am wanting to consolidate multiple menus within this one select dropdown and have tried this:

<?php dropdown_menu( array('dropdown_title' => '-- Main Menu --', 'container' => 'div',  'theme_location'=>'main_menu', 'theme_location'=>'top_menu', 'theme_location'=>'footer_menu') ); ?>

Unfortunately, this still only shows the last menu 'footer_menu' instead of combining all three menus. Any ideas on how I can edit the above code correctly so that all menus will show in the select box as one?

Any help would be greatly appreciated.

回答1:

You can use a code that I use regularly in some projects, something simple that pueds customize jQuery

You can change the value to set a specific div

var $mainNav    = $('#menu').children('ul'),

Code complete jQuery

(function($) {
    var $mainNav    = $('#menu').children('ul'),
        optionsList = '<option value="" selected>Navigate...</option>';

    // Regular nav
    $mainNav.on('mouseenter', 'li', function() {
        var $this    = $(this),
            $subMenu = $this.children('ul');
        if( $subMenu.length ) $this.addClass('hover');
        $subMenu.hide().stop(true, true).fadeIn(200);
    }).on('mouseleave', 'li', function() {
        $(this).removeClass('hover').children('ul').stop(true, true).fadeOut(50);
    });
    // Responsive nav
    $mainNav.find('li').each(function() {
        var $this   = $(this),
            $anchor = $this.children('a'),
            depth   = $this.parents('ul').length - 1,
            indent  = '';
        if( depth ) {
            while( depth > 0 ) {
                indent += '--';
                depth--;
            }
        }
        optionsList += '<option value="' + $anchor.attr('href') + '">' + indent + ' ' + $anchor.text() + '</option>';
    }).end().after('<select class="responsive-nav">' + optionsList + '</select>');
    $('.responsive-nav').on('change', function() {
        window.location = $(this).val();
    });
})(jQuery);


回答2:

You could either implement this plugin http://wordpress.org/extend/plugins/responsive-select-menu/ or give http://tinynav.viljamis.com/ a trial.