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