最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

menus - How to avoid wp_nav_menu() ID conflict?

programmeradmin1浏览0评论

How do I avoid ID conflicts if I use the same menu TWICE on one page.

FIRST

wp_nav_menu( array( 'sort_column' => 'menu_order', 
'theme_location'=>'menu', 'menu_class'=>'menu', 'menu_id'=>'menu' ) );

SECOND:

wp_nav_menu( array( 'sort_column' => 'menu_order', 
'theme_location'=>'menu', 'menu_class'=>'menu2', 'menu_id'=>'menu2' ) );

ID conflicts are like "Duplicate ID menu-item-2456"... Any solutions?

How do I avoid ID conflicts if I use the same menu TWICE on one page.

FIRST

wp_nav_menu( array( 'sort_column' => 'menu_order', 
'theme_location'=>'menu', 'menu_class'=>'menu', 'menu_id'=>'menu' ) );

SECOND:

wp_nav_menu( array( 'sort_column' => 'menu_order', 
'theme_location'=>'menu', 'menu_class'=>'menu2', 'menu_id'=>'menu2' ) );

ID conflicts are like "Duplicate ID menu-item-2456"... Any solutions?

Share Improve this question asked Jun 15, 2012 at 18:08 AtadjAtadj 2,1828 gold badges29 silver badges40 bronze badges 2
  • You are not supposed to use the same theme_location, can you define a second one? – Mario Peshev Commented Jun 15, 2012 at 18:25
  • This cannot happen with different 'theme_location's. I’ll remove my answer. – fuxia Commented Jun 15, 2012 at 18:54
Add a comment  | 

1 Answer 1

Reset to default 2

The solution is not to call the same 'theme_location' more than once. Theme location is intended to represent an explicit location within the template.

Just register a separate 'theme_location' for each separate location within the template that you want to display a nav menu.

Consider your chosen 'theme_location' names to be semantic names, representing the template location of the menu. You could use 'primary' and 'secondary', or 'header' and 'footer', etc.:

<?php
function wpse55380_setup_theme() {

    // Register nav menu locations
    register_nav_menus( array(
        'header' => 'Header Menu',
        'footer' => 'Footer Menu'
    ) );
}
add_action( 'after_setup_theme', 'wpse55380_setup_theme' ); 

...or:

<?php
function wpse55380_setup_theme() {

    // Register nav menu locations
    register_nav_menus( array(
        'primary'   => 'Primary Header Menu',
        'secondary' => 'Secondary Header Menu'
    ) );
}
add_action( 'after_setup_theme', 'wpse55380_setup_theme' ); 

Then, it is up to the end user to assign custom menus to each Theme Location.

发布评论

评论列表(0)

  1. 暂无评论