• Hey y’all. I’ve run searches and read quite a bit on this, and honestly I can’t figure out what it is that I’m missing. I have the following code in my custom theme directory:

    functions.php

    $fn = new MyNamespace\MyClass();
    add_action('init',array($fn,'registerMenus'));

    MyClass.php

    namespace MyNamespace;
    class MyClass{
        public function registerMenus(){
            \register_nav_menus(array(
                'header-menu'=>__('Header Menu')
            ));
        }
    }

    header.php

    <nav id="header-navigation" role="navigation">
        <?php
        if(has_nav_menu('header-menu')){
            print("<p>Right here would be good, ya</p>");
            wp_nav_menu(array('theme_location'=>'header-menu'));
        }else{
            print("<p>No menu, no happy.</p>");
        }
        ?>
    </nav>

    I assigned 2 CPTs to the Header Menu (named ‘Corporate Menu’) in the Appearance > Menus page of my WP back-end. I get the ‘Right here would be good, ya’ message printed out exactly where I want it. However, I’ve got no menu.

    Can anyone see what I’m dong wrong here? My code looks like the examples I’ve found online and I don’t recall reading anything about a menu system revamp in 4.0, and I’m stymied…

Viewing 7 replies - 1 through 7 (of 7 total)
  • Just as a question, why is this is all done within a class?

    Are you building a plugin or something?

    Also what is the \ next to register nav menu?

    Thread Starter maxxd

    (@maxxd)

    Actually, I’m working on a theme and honestly I’ve just gotten very used to working with classes in php. The ‘\’ is used in conjunction with namespaces to back out of the current namespace (in this case MyNamepsace).

    All that aside, just in case I actually commented out the call to the class method and put the function directly into the functions.php file, and did this:

    add_action('init','registerMenus');
    function registerMenus(){
        register_nav_menus(array(
            'header-menu'=>__('Header Menu')
        ));
    }

    Still no menu…

    Have you tried:

    wp_nav_menu(
    	'menu' => 'Header Menu',
    	// do not fall back to first non-empty menu
    	'theme_location' => '__no_such_location'
    	// do not fall back to wp_page_menu()
    	'fallback_cb' => false
    );

    I think you may need to pass in the name of the menu. Could be worth a shot.

    Thread Starter maxxd

    (@maxxd)

    Thank you – I gave it a shot; unfortunately I got no menu whether I use the class or plain function.

    Trying to set things up on a localhost installation for testing.

    Tested and confirmed working on a localhost installation:

    Inside functions.php:

    register_nav_menus( array(
    	'footer_menu' => 'My Custom Footer Menu'
    ) );

    Inside of a template file, tested inside of my footer.php template:

    <nav id="header-navigation" role="navigation">
        <?php
        if(has_nav_menu('footer_menu')) {
    			wp_nav_menu( array(
    			'theme_location' => 'footer_menu',
    			'menu_class' => 'nav',
    			'container' => ''
    		) );
        }else{
            print("<p>No menu, no happy.</p>");
        }
        ?>
    </nav>

    I’m sure you can tweak things to fit into your class, but I’ve tested and can get the menu successfully displaying.

    I would try unsetting the menu locations in the dashboard (from the menu screen in the dashboard, scroll all the way to the bottom and uncheck the location ) and then re-saving the option. Then re-set the locations and refresh the page.

    That seemed to do the trick for me on localhost, similar to refreshing the permalinks when you add a new CPT.

    Evan

    Thread Starter maxxd

    (@maxxd)

    Evan, thank you for taking the time to help out!

    It’s got to be something extremely simple that’s killing me – it’s still not working. I tried changing the name of the menu to header_menu and re-assigning my CPTs to it on the menus page. Nothing. So I assigned a page to the custom menu just to see if it was my CPT that was throwing things off. I’ve still got no menu…

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Custom menu not appearing’ is closed to new replies.