我一直在使用in\\u category函数在侧边栏上显示一组菜单。然而,更好的方法是将此菜单显示在顶部,作为导航菜单--以自定义菜单的形式。
问题:
我已经有了一个自定义菜单,但是每当读者访问某个特定类别的帖子时,有没有办法用另一个自定义菜单来替换这个自定义菜单?
示例:
在主页上,这是菜单:
家|猫|狗
在标有CAT类别的帖子上,以下是菜单:
住宅| 1类| 2类
据我所知,这是当前的代码:
if ( function_exists(\'wp_nav_menu\') ) {
add_theme_support( \'nav-menus\' );
register_nav_menus( array( \'primary-menu\' => __( \'Primary Menu\', \'woothemes\' ) ) );
register_nav_menus( array( \'top-menu\' => __( \'Top Menu\', \'woothemes\' ) ) );
}
还有。。。
<?php
if ( function_exists( \'has_nav_menu\' ) && has_nav_menu( \'primary-menu\' ) ) {
echo \'<h3>\' . woo_get_menu_name( \'primary-menu\' ) . \'</h3>\';
wp_nav_menu( array( \'sort_column\' => \'menu_order\', \'container\' => \'ul\', \'menu_id\' => \'main-nav\', \'menu_class\' => \'nav fl\', \'theme_location\' => \'primary-menu\' ) );
} else {
?>
<ul id="main-nav" class="nav fl">
<?php
if ( get_option( \'woo_custom_nav_menu\' ) == \'true\' ) {
if ( function_exists( \'woo_custom_navigation_output\' ) ) { woo_custom_navigation_output( \'name=Woo Menu 1\' ); }
} else { ?>
<?php if ( is_page() ) { $highlight = \'page_item\'; } else { $highlight = \'page_item current_page_item\'; } ?>
<li class="<?php echo esc_attr( $highlight ); ?>"><a href="<?php echo esc_url( home_url( \'/\' ) ); ?>"><?php _e( \'Home\', \'woothemes\' ); ?></a></li>
<?php wp_list_pages( \'sort_column=menu_order&depth=6&title_li=&exclude=\' ); ?>
<?php } ?>
</ul><!-- /#nav -->
<?php } ?>
</section><!-- /.menus -->
最合适的回答,由SO网友:Pontus Abrahamsson 整理而成
添加名为“类别菜单”的新菜单:
if( function_exists( \'wp_nav_menu\' ) )
{
add_theme_support( \'nav-menus\' );
register_nav_menus( array(
\'primary-menu\' => __( \'Primary Menu\', \'woothemes\' )
));
register_nav_menus( array(
\'top-menu\' => __( \'Top Menu\', \'woothemes\' )
));
register_nav_menus( array(
\'category-menu\' => __( \'Category Menu\', \'woothemes\' )
));
}
并将其与条件
is_category():
if( function_exists( \'has_nav_menu\' ) && has_nav_menu( \'primary-menu\' ) )
{
// On category \'Cheese\'
if( is_category( \'Cheese\' ) )
{
echo \'<h3>\' . woo_get_menu_name( \'category-menu\' ) . \'</h3>\';
wp_nav_menu( array(
\'sort_column\' => \'menu_order\',
\'container\' => \'ul\',
\'menu_id\' => \'main-nav\',
\'menu_class\' => \'nav fl\',
\'theme_location\' => \'category-menu\'
));
}
else
{
echo \'<h3>\' . woo_get_menu_name( \'primary-menu\' ) . \'</h3>\';
wp_nav_menu( array(
\'sort_column\' => \'menu_order\',
\'container\' => \'ul\',
\'menu_id\' => \'main-nav\',
\'menu_class\' => \'nav fl\',
\'theme_location\' => \'primary-menu\'
));
}
}