简体   繁体   中英

How come my custom menu isn't showing in Wordpress?

I am trying to spit this menu out in my template on every page, in my theme's header.php file:

在此处输入图片说明

Note: The theme comes with 3 locations for menus, but I will be using these for different menus, therefore I can't use these pre-existing locations.

I tried to register it in functions.php using this:

function register_my_menu() {
      register_nav_menu('utility-menu',__( 'Utility' ));
}
add_action( 'init', 'register_my_menu' );

This registers it, var_dump( get_registered_nav_menus() ); returns:

array(4) {
  ["menu-header"]=>
  string(11) "Menu Header"
  ["menu-top"]=>
  string(8) "Menu Top"
  ["wpv-push-menu"]=>
  string(9) "Push Menu"
  ["utility-menu"]=>
  string(7) "Utility"
}

But this doesn't seem to link it. I'm doing things the wrong way obviously, so can someone point out what I'm not understanding? I want to know how to properly fetch that menu the right way.

wp_nav_menu( array( 'name' => 'Utility' ) );
wp_nav_menu( array( 'theme_location' => 'utility-menu' ) );

Seems to pull in a different menu.

You should register your menu during after_setup_theme , not init .

add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu(){
  register_nav_menu( 'utility-menu', 'Utility Menu' );
}

Then, on your Menus admin, make sure to assign whatever menu you want to the Utility menu (you'll see it under Menu Settings by Menu Header, Menu Top, etc.

After doing those two things, you'll call it like you did in the second example:

wp_nav_menu( array(
  'theme_location' => 'utility-menu'
) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM