Incorrect usage of wp_nav_menu

Using the WordPress navigation menus is quite straightforward. First a theme location is registered. Here is an example taken from Twenty Twelve:

register_nav_menu( 'primary', __( 'Primary Menu', 'twentytwelve' ) );

Second, the navigation menu is displayed with ‘wp_nav_menu’. Here again is an example taken from Twenty Twelve:

wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) );

The function accepts quite a lot of (optional) arguments, but the most important one is ‘theme_location’. When registering only one theme location with ‘register_nav_menu’, WordPress appears to work if it is not specified in the ‘wp_nav_menu’ call, but it is a wrong assumption. Just make the following test (with Polylang inactive to be certain that it does not interefere in your test):

  • don’t specify the theme_location parameter in your ‘wp_nav_menu’ call
  • create a first menu in appearance->menus and save it
  • create a second menu and save it
  • assign the second menu to your unique theme location

You will see that WordPress displays the first created menu instead of the second one. That’s not a WordPress bug ! But a frequent mistake found in themes (including Premium themes). And of course since Polylang needs at least one menu per language, such theme conflicts.

So to theme developpers, please, always specify the ‘theme_location’ parameter in your ‘wp_nav_menu’ calls…

Picture illustrating the article by Alexas_Fotos and licensed under CC0 Public Domain.