简体   繁体   中英

wp_nav_menu not working wordpress

I am creating a theme for my wordpress blog, I am struct on the code to display custom menus

in my functions.php I have wrote:

function register_my_menus(){
register_nav_menus(
    array('menu-1' => __('Primary Menu'),
    )
);
}

add_action('init', 'register_my_menus');

this is my header.php

 if ( has_nav_menu( $location ) ) {
     wp_nav_menu(array( 'theme_location' => 'menu-1'));
 }

the problem is that when I am setting a menu from wordpress to Primary Menu, no menu is displaying and also the content after menu is not displaying, please help me where I am wrong

You've used the has_nav_menu() command, by the looks of it, copy/pasted from the Codex, without giving it a parameter. Try this:

Instead of

if ( has_nav_menu( $location ) ) {
    wp_nav_menu(array( 'theme_location' => 'menu-1'));
}

This:

if ( has_nav_menu( 'menu-1' ) ) {
    wp_nav_menu(array( 'theme_location' => 'menu-1'));
}

Then, provided you've actually made a menu for the location in the admin panel, and assigned it to this location, it should work as far as I can see.

您好mandeep singh,您是否已将菜单分配给wordpress管理面板中的“主菜单”

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