Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hello all you helpful people :)

I am using a self modified version of the 2012 theme and I would like to be able to display a different main navigation menu based on the category I am viewing.

Firstly is there a plugin for such a thing as a GUI would be easier for me to get to grips with

If not I'm guessing it will be PHP in the header.php file? I already have if statements for custom headers per category so I can add the additional code for switching menus in here.

Any help is great :)

My existing menu code is:

<nav id="site-navigation" class="main-navigation" role="navigation">
            <h3 class="menu-toggle"><?php _e( 'Menu', 'twentytwelve' ); ?></h3>
            <a class="assistive-text" href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentytwelve' ); ?>"><?php _e( 'Skip to content', 'twentytwelve' ); ?></a>
            <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ); ?>
        </nav><!-- #site-navigation -->

Main menu is called 'Talkative' Custom menu is called 'Pirtek'

I cannot see in the existing code what would be changed unless I have to add additional function calls to wp_nav_menu for example

<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' 'menu' => 'Talkative') ); ?>
share|improve this question
    
Plugin recommendation is beyond the scope of this community. Can you also update the code, you have already tried to make this work? –  Maruti Mohanty Nov 5 '13 at 10:06
    
If you have successfully added custom headers per category, then you can also try wp_nav_menu for displaying different menus for different categories codex.wordpress.org/Function_Reference/wp_nav_menu –  Maruti Mohanty Nov 5 '13 at 10:24
1  
hah plugins - of course ;). I'm going to have a go myself at using the wp_nav_menu inside my php if statements and see what I can get to work :) i'll let you know how I get on –  Sam James Nov 5 '13 at 12:26
    
Added additional coding and queries to initial post. Almost cracked it but CSS is having issues and stuff :'(. Close though! –  Sam James Nov 5 '13 at 14:22

1 Answer 1

up vote 0 down vote accepted

Try the following

<nav id="site-navigation" class="main-navigation" role="navigation">
    <h3 class="menu-toggle"><?php _e( 'Menu', 'twentytwelve' ); ?></h3>
    <a class="assistive-text" href="#content" title="<?php esc_attr_e( 'Skip to content', 'twentytwelve' ); ?>"><?php _e( 'Skip to content', 'twentytwelve' ); ?></a>
    <?php
    //Replace it with the id/slug/name(anything you want)
    if( is_category( 'Talkative' ) ) {
        wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu' => 'Talkative') );
    } elseif( is_category( 'Pirtek' ) ) {
        wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu' => 'Pirtek') );
    } else {
        wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) );
    }
    ?>
</nav><!-- #site-navigation -->

Haven't tried it, but this should work.

For details check is_category() and wp_nav_menu()

share|improve this answer
1  
Boom! Perfect! I used has_category as some of the posts overlap categories you see. I was also just using basic menu wp_nav_menu() calls rather than using the CSS as the coding above. Guru! –  Sam James Nov 5 '13 at 15:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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