1

I have been using in_category function to display a set of menu on the sidebar. However, a much better way would be to show this menu on the top, as a navigation menu -- in the form of custom menus.

Question:

I already have a custom menu, but is there a way I could replace this custom menu with another one whenever a reader visits a post from a particular category?

Example:

On homepage, this is the menu:

Home | Cat | Dog

On a post tagged with CAT category, this is the menu:

Home | Cat 1 | Cat 2

From my understanding, this is the current code:

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' ) ) );
}

And...

<?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 Answer 1

2

Add a new menu called category-menu:

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' ) 
    ));
}

And output it with the conditional 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' 
        ));
    }
}
3
  • Had a hard time playing with the codes (missed adding an additional "}" at the end). I am glad this is possible. You are a genius! Thank you so much...
    – Sid
    Commented Jul 28, 2013 at 16:38
  • 1
    Glad you did get it to work for you! Commented Jul 28, 2013 at 16:39
  • In what file should this change be made?
    – Benchik
    Commented Jun 27, 2014 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.