如果我将“echo”参数值设置为“true”:
<?php if( has_nav_menu( \'primary-menu\' ) ) {
wp_nav_menu( $menuParameters = array(
\'theme_location\' => \'primary-menu\',
\'container\' => \'\',
\'container_class\' => false,
\'container_id\' => false,
\'menu\' => \'Primary\',
\'menu_id\' => \'test\',
\'menu_class\' => \'nav navbar-nav navbar-right\',
\'echo\' => true,
\'fallback_cb\' => \'wp_page_menu\',
\'before\' => \'\',
\'after\' => \'\',
\'link_before\' => \'\',
\'link_after\' => \'\',
\'items_wrap\' => \'<ul id="%1$s" class="%2$s">%3$s</ul>\',
\'depth\' => 0,
\'walker\' => \'\'
) );
wp_nav_menu (array(\'theme_location\' => \'primary-menu\'));
} ?>
我得到以下输出:
<ul id="test" class="nav navbar-nav navbar-right">
<li class="current-menu-item"><a href="http://localhost/test/">Home</a></li>
<li><a href="http://localhost/test/contact/">Contact</a></li>
</ul>
<ul id="menu-primary" class="menu">
<li class="current-menu-item"><a href="http://localhost/test/">Home</a></li>
<li><a href="http://localhost/test/contact/">Contact</a></li>
</ul>
第一个是期望的结果,最后一个是WP默认输出,这是一个难题。有没有办法通过改变我的数组来摆脱它?
最合适的回答,由SO网友:M-R 整理而成
您有两次调用wp\\u nav\\u菜单功能
<?php if( has_nav_menu( \'primary-menu\' ) ) {
wp_nav_menu( $menuParameters = array(
\'theme_location\' => \'primary-menu\',
\'container\' => \'\',
\'container_class\' => false,
\'container_id\' => false,
\'menu\' => \'Primary\',
\'menu_id\' => \'test\',
\'menu_class\' => \'nav navbar-nav navbar-right\',
\'echo\' => true,
\'fallback_cb\' => \'wp_page_menu\',
\'before\' => \'\',
\'after\' => \'\',
\'link_before\' => \'\',
\'link_after\' => \'\',
\'items_wrap\' => \'<ul id="%1$s" class="%2$s">%3$s</ul>\',
\'depth\' => 0,
\'walker\' => \'\'
) );
// Following is outputing second menu html
//wp_nav_menu (array(\'theme_location\' => \'primary-menu\'));
} ?>