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.

I am trying to make each individual tab on my primary menu link to the secondary menu so that in effect each individual primary tab leads to an individual set of tabs in the secondary menu rather than it just remaining static.

I hope you can help and I'm not an advanced WP user so its ok to use simple terms.

Update

I've added the code but it has shown up on my webpage and in the dashboard so I must have it wrong so I've pasted it below so you can see. I'm not sure how to find the primary slug but I think its what I've added in place of primary.

function km_dynamic_secondary_menu_reg() {
    global $km_nav_menus;
    $theme_location = 'nav-menus'; 
    $locations = get_nav_menu_locations();
    $menu_items = wp_get_nav_menu_items( $locations[$theme_location] );
    if ( isset($menu_items) && !empty($menu_items) ) {
        $km_nav_menus = array();
        foreach( $menu_items as $item ) {
            if ( 'post_type' == $item->type ) {
                $post_type = ( 'page' == $item->object ) ? $item->object : 'post';
                $km_nav_menus[get_post_field( 'post_name',  $item->object_id ).'-'. $post_type] = '"'.$item->title . '" Secondary Menu';
                register_nav_menus( $km_nav_menus );
            } elseif ( 'taxonomy' == $item->type ) {
                $km_nav_menus[get_term_field( 'slug', $item->object_id, $item->object ).'-tax'] = '"'.$item->title . '" Secondary Menu';
                register_nav_menus( $km_nav_menus );
            } else {
                //Your default fallback if needed
            }

        }
    }
}
add_action( 'init', 'km_dynamic_secondary_menu_reg' );
share|improve this question
    
A bit more detail is required in order to understand what you're trying to achieve. Perhaps even a link to a page that illustrates this. –  KalenGi Jul 23 '12 at 23:27
    
Hi www.guardian.co.uk is a good illustration of how I would like the menus to act. And my website is karenmawer.com - I have the template on there. Say I have sport, politics and food as tabs on my primary menu. When I click on food I want the secondary menu to have food related tabs and not say tennis which would only show if the sport tab was clicked. I want them to be relative to the primary tab. Hope this explains it a little better. Thank you. –  Karen Mawer Jul 23 '12 at 23:37
    
@kalengi link link –  Karen Mawer Jul 24 '12 at 0:16
    
I've tested @joshua's code and it actually works! –  KalenGi Jul 24 '12 at 10:05
    
@KarenMawer try wrapping the above code in php tags <?php (Above code goes here) ?>. I also doubt whether the theme location you have given is right. –  Joshua Abenazer Jul 25 '12 at 6:02
add comment

1 Answer 1

I am guessing you are gonna use pages or categories in the primary menu. The following code would need to be altered to work for custom taxonomies ( should work fine with custom post types also ).

Put the following code in your themes functions.php:

function km_dynamic_secondary_menu_reg() {
    global $km_nav_menus;
    $theme_location = 'primary'; // replace with the primary registered nav menu
    $locations = get_nav_menu_locations();
    $menu_items = wp_get_nav_menu_items( $locations[$theme_location] );
    if ( isset($menu_items) && !empty($menu_items) ) {
        $km_nav_menus = array();
        foreach( $menu_items as $item ) {
            if ( 'post_type' == $item->type ) {
                $post_type = ( 'page' == $item->object ) ? $item->object : 'post';
                $km_nav_menus[get_post_field( 'post_name',  $item->object_id ).'-'. $post_type] = '"'.$item->title . '" Secondary Menu';
                register_nav_menus( $km_nav_menus );
            } elseif ( 'taxonomy' == $item->type ) {
                $km_nav_menus[get_term_field( 'slug', $item->object_id, $item->object ).'-tax'] = '"'.$item->title . '" Secondary Menu';
                register_nav_menus( $km_nav_menus );
            } else {
                //Your default fallback if needed
            }

        }
    }
}
add_action( 'init', 'km_dynamic_secondary_menu_reg' );

You would need to assign the primary menu slug to $theme_location in the above code.

After executing the above code you should see extra registered menus on your Dashboard->Appearance->Menus Section based on the items in your primary menu. This would be dynamic and would be dependent on your primary menu.

Now the last thing you need to do is output the following code wherever you want to show the secondary menu.

global $km_nav_menus;
if ( function_exists( 'wp_nav_menu' ) ) {
    foreach( $km_nav_menus as $key => $value ) {
        $p_menu_item = explode( '-', $key );
        if ( 'tax' == $p_menu_item[count($p_menu_item)-1] ) {
            unset($p_menu_item[count($p_menu_item)-1]);
            if ( is_category( implode( '-', $p_menu_item ) ) || is_tag( implode( '-', $p_menu_item ) ) ){
                if ( has_nav_menu( $key ) )
                    wp_nav_menu( array( 'theme_location' => $key ) );
                break;
            }
        }
        if ( 'page' == $p_menu_item[count($p_menu_item)-1] ) {
            unset($p_menu_item[count($p_menu_item)-1]);
            if ( is_page(implode( '-', $p_menu_item ) ) ){
                if ( has_nav_menu( $key ) )
                    wp_nav_menu( array( 'theme_location' => $key ) );
                break;
            }
        }
        if( 'post' == $p_menu_item[count($p_menu_item)-1] ) {
            unset($p_menu_item[count($p_menu_item)-1]);
            if ( is_single(implode( '-', $p_menu_item ) ) ){
                if ( has_nav_menu( $key ) )
                    wp_nav_menu( array( 'theme_location' => $key ) );
                break;
            }
        }
    }
}
share|improve this answer
    
The only step missing here is that you should create new menus to assign to each of the dynamically generated secondary menu containers. –  KalenGi Jul 24 '12 at 10:08
add comment

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.