简体   繁体   English

为什么我的自定义菜单没有显示在Wordpress中?

[英]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: 我试图在我的主题的header.php文件的每个页面的模板中吐出此菜单:

在此处输入图片说明

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. 注意:主题附带3个菜单位置,但是我将这些位置用于不同的菜单,因此无法使用这些预先存在的位置。

I tried to register it in functions.php using this: 我试图使用以下方法在functions.php注册它:

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() ); 这将注册它, 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 . 您应该在after_setup_theme而不是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'
) );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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