在不同类别页面上显示不同的自定义菜单

时间:2013-07-28 作者:Sid

我一直在使用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 -->

1 个回复
最合适的回答,由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\' 
        ));
    }
}

结束

相关推荐

编辑“Kahi‘s Highlight Used Categories”插件代码--在帖子页面高亮显示父子类别

我试图突出显示(使用css)所选帖子中使用的子类别和父类别。因此,我找到了这个插件“Kahi’s Highlight Used Categories”。插件网站:http://kahi.cz/wordpress/highlight-used-categories-plugin/它的作用。。。它将“used cat”类和“used cat parent”类分别添加到子类别和主类别中进行样式化。问题是:我的网站(http://www.mpn.p.ht/wp_br2/), 有三大类:“男性”(意为男性)、“女性