How to show primary menu of main site throughout m

2019-08-21 20:22发布

I have managed to switch the primiary navigation menu of my subsites to show the main site primary navigation.

However, it renders above the site-header instead of in the menu location dictated in the code.

Here is the code I currently have:

    function wp_multisite_nav_menu() {
    global $blog_id;
}
    if ( ! is_multisite() || 2 == $blog_id ) {

    switch_to_blog( 1 );

    wp_nav_menu( array(
        'menu'              => 2,
        'fallback_cb'       => false,
        'menu_class'        => 'genesis-nav-menu',
        'theme_location'    => 'Primary Navigation Menu',

    )); 

    restore_current_blog(); 

}

I was expecting the menu to be placed in the 'Primary Navigation Menu' location.

What have I missed?

Any clarity is appreciated.

UPDATE

I managed to figure it out for my primary and secondary menus however how do get the site title to change to the main site title and hyperlink?

Here is the code I currently have minus the site title switch

//*Multisite global menus

//*Primary global menu
add_action('genesis_after_header', 'primary_menu_switch');
function primary_menu_switch() {
    global $blog_id;
    if ( ! is_multisite() || 2 == $blog_id ) {
    switch_to_blog( 1 );

    wp_nav_menu( array(
        'menu'              => 2,
        'fallback_cb'       => false,
        'menu_class'        => 'genesis-nav-menu',
        'theme_location'    => 'primary'
    ) );

    restore_current_blog(); 
}
}


//*Secondary global menu
add_action('genesis_header_right', 'secondary_menu_switch');
function secondary_menu_switch() {
    global $blog_id;
    if ( ! is_multisite() || 2 == $blog_id ) {
    switch_to_blog( 1 );

    wp_nav_menu( array(
        'menu'              => 17,
        'fallback_cb'       => false,
        'menu_class'        => 'genesis-nav-menu menu-primary responsive-menu',
        'theme_location'    => 'primary'

        ));         
    restore_current_blog(); 
}
}

//*Use main site title

function site_title_switch() {
    global $blog_id;
    if ( ! is_multisite() || 2 == $blog_id ) {

    switch_to_blog( 1 );



   restore_current_blog();  

}
} 

I am a complete novice so please excuse the hack job.

Your insights are appreciated.

1条回答
ら.Afraid
2楼-- · 2019-08-21 20:54

This is an answer to the updated question, not the one in the title.

This should do the trick, if you put it in a network activated plugin. Read the comments to see what it does exactly. It might not work depending on how your theme is made. I made it for the Twenty Eleven theme.

Keep in mind that it will change the home URL everywhere where it is called with a path '/', not only in the header.

add_filter( 'option_blogname', 'function_to_filter_the_blogname' );

// Changes the blog name of all sites that are not the main one to the name of the main one, only outside of the admin panel
function function_to_filter_the_blogname( $name ) {
    $main_site_id = get_main_site_id();
    if ( get_current_blog_id() != $main_site_id && ! is_admin() ) {
        return get_blog_option( $main_site_id, 'blogname' );
    }
    return $name;
}

add_filter( 'home_url', 'function_to_filter_the_home_url', 10, 4 );

// Changes the home URL of all sites that are not the main one to the home URL of the main one, only outside of the admin panel and only when the path is '/'
function function_to_filter_the_home_url( $url, $path, $orig_scheme, $blog_id ) {
    $main_site_id = get_main_site_id();
    if ( $blog_id != $main_site_id && ! is_admin() && '/' == $path ) {
        return get_blog_option( $main_site_id, 'home' );
    }
    return $url;
}
查看更多
登录 后发表回答