Not to long ago I had a request for a MultiSite WordPress network where all blogs in the network needed to share the same main navigation menus. When I searched around I didn’t see anyone talking about this so I figured I would post my solution to the problem.
Within your theme files you can use the WordPress function wp_nav_menu(); which will output the appropriate navigational menu depending on the arguments you supply. You can learn more about this function here. The problem is that in a MultiSite network the navigation is output depending on the current blog being viewed. This makes sense and should be the default behavior. But what if you want to keep a consistent navigation throughout all blogs in the network? You can use the WordPress MU function switch_to_blog() as shown below.
//store the current blog_id being viewed global $blog_id; $current_blog_id = $blog_id; //switch to the main blog which will have an id of 1 switch_to_blog(1); //output the WordPress navigation menu wp_nav_menu( //add your arguments here ); //switch back to the current blog being viewed switch_to_blog($current_blog_id);
Before you can output the navigation you first have to create it by visiting Appearance->Menus. But once you get your main menu created placing the above code in your WordPress theme file will allow you to share your main blogs menu amongst all blogs on the network. If you have any questions or comments about this solution feel free to post them in the comments section below.



Where would I put the code you posted above?
Place this code directly in your theme file, where your menu is output.
Found this via google where often it’s easier to search for a phrase than dig through all the functions at the codex.
Thanks for posting. Perfect.
Thank you very much I tried another tutorial http://wpmututorials.com/plugins/networkwide-menu/ and was not successfull. Your example worked great. Quick ? Do you know of a plugin or a way to get the like function on this page http://wpdevel.wordpress.com/2011/05/25/jquery-updates-in-wordpress-3-2/ onto a custom wordpress site? I have seen this sharedaddy like button on other blogs but have come up short on my research. Thanks for any input.
Glad this helped. Not sure about the Like button, I would have to do some research.
Thank you for sharing this code! Been looking all over for this place till a co-worker mentioned this site. Could this be done in function with multiple wp_nav_menu? Something like this:
// Global Nav Menu from blog ID 1
function global_nav_menu() {
//store the current blog_id being viewed
global $blog_id;
$current_blog_id = $blog_id;
//switch to the main blog which will have an id of 1
switch_to_blog(1);
//output the WordPress navigation menu
register_nav_menus( array(
‘misc’ => __( ‘Misc Navigation’, ‘twentyten’ ),
‘primary’ => __( ‘Primary Navigation’, ‘twentyten’ ),
) );
//switch back to the current blog being viewed
switch_to_blog($current_blog_id);
}
Then output in header.php like this
”, ‘menu_class’ => ” , ‘menu_id’ => ”, ‘theme_location’ => ‘misc’, ‘fallback_cb’ => ” ) ); ?>
”, ‘menu_class’ => ” , ‘menu_id’ => ”, ‘theme_location’ => ‘primary’, ‘fallback_cb’ => ” ) ); ?>
Not sure if this code would work, but could you please point me the right direction? Much appreciated!
Long
You can use switch_to_blog() as many times as you want, then you can use wp_nav_menu() to output whatever menu you want from the “current” blog.
[...] just ran across Oste Design which says, I think, approximately the same thing. This goes in the theme [...]
Thank you very much …
thank you for the shared code, I have a doubt,
we have to put this exact code in theme.php that is located in wp-includes?
You would want to place this code in one of your theme files located in wp-content/themes. It could possibly go inside header.php.
Perfect, thank you very much.
[...] just ran across Oste Design which says, I think, approximately the same thing. This goes in the theme [...]
Dude, thank you. This is just perfect. Does exactly what we needed it to do, so we now have a global navigation controlled from the primary blog’s choices from the built in WordPress Menus system.
This is bookmarked for eternity, never let this sites hosting or domain lapse! ;0)