• I need to output the primary menu for a plugin I’m working on and I was able to output the menu with:
    echo wp_nav_menu();

    But that outputs whichever menu is alphabetically saved first. The format of the output isn’t the issue here like most people using wp_nav_menu() but the ability to make it output which menu is selected by the user as the primary. What argument would I pass through? I’ve tried things like this, with no luck:

    echo wp_nav_menu(array('menu' => 'primary');

    In the end it is a menu that is in a hidden div that I use the content of for a javascript function.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter squidstack

    (@squidstack)

    I think this solved it, it has in a few of my testing sites. Does this look like a universal way to alway display the primary menu:

    echo wp_nav_menu(array('theme_location' => 'primary'))

    Moderator bcworkz

    (@bcworkz)

    ‘primary’ only works if the theme has registered such a location. I suspect a lot of themes do this, but I don’t think ‘primary’ is required. A theme can choose any location name, ‘main’ perhaps, or maybe not even setup a location at all if it’s some sort of minimalist theme.

    I think to be most compatible, you need to first get the locations with get_nav_menu_locations() and search through the locations for ‘primary’ before using it. If there is none, search for other likely names like main, first, top, upper, header, etc. If no likely candidate is found, you may as well call wp_nav_menu() with no parameters to get the first menu with items in it. If a theme is well written, this should result in the proper menu anyway.

    BTW, while it works either way, by default you do not need to echo wp_nav_menu(), it does its own output. You need an 'echo'=> false, argument to get it to return something instead of echoing.

    Thread Starter squidstack

    (@squidstack)

    that’s great info, still learning wordpress’ inner workings.

    I can get all the locations and their menus output with this:

    $menuItemLocations = get_nav_menu_locations();
    foreach ($menuItemLocations as $key => $value){
        wp_nav_menu(array('theme_location' => $key));
    };

    thanks bcworkz!

    Thread Starter squidstack

    (@squidstack)

    After some more testing I found that even though I would get all the locations with the previous code I could get duplicates if the menus were used in more than one location. This solved that:

    function menuFindOutput(){
        $menuItemLocations = get_nav_menu_locations();
        $dchecked = array();
        $valCheck = array();
        foreach($menuItemLocations as $key => $value) {
             $valCheck[] = $value;
    		foreach($valCheck as $curVal) {
    			if (in_array($curVal, $dchecked)) {
    			}
    			else {
    				$dchecked[] = $curVal;
    				wp_nav_menu(array('theme_location' => $key));
    			}
    		}
    	}
    }

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Output primary nav with wp_nav_menu()’ is closed to new replies.