WordPress Multisite: Have the same header and footer of main-blog on all sub-blogs

I’m trying to do the following:

I have a Main-Blog and dozens of subblogs.
I want all the subblogs to use the same theme (or child-theme, not sure yet) but have the same navigationitems of the mainblog on all subblogs? The same for the footer.

Read More

How’d I do that?

This is my Main-Blog and Landing-Page

enter image description here

If I click on Subblogs and chose a Subblog i just want the content area to be affected, the header and the footer should stay the same over the entire network.

enter image description here

Inside the content-area of a subblog I’d than like to use the pages and posts of the subblog.

What is the right way to do this?


Update:

<ul role="navigation">
    <?php

        //wp_list_pages('title_li=&depth=1&exclude=42,311');

        $args = array(
            'authors'      => '',
            'child_of'     => 0,
            'date_format'  => get_option('date_format'),
            'depth'        => 1,
            'echo'         => 0,
            'exclude'      => '42,311',
            'include'      => '',
            'link_after'   => '',
            'link_before'  => '',
            'post_type'    => 'page',
            'post_status'  => 'publish',
            'show_date'    => '',
            'sort_column'  => 'menu_order, post_title',
            'title_li'     => '', 
            'walker'       => ''
        );

        $menu = wp_list_pages( $args );
        update_option('network_menu', $menu);
        echo $menu;
    ?>
</ul>

in my child-theme I do:

<ul role="navigation">
    <?php 
        $menu = get_option('network_menu');
            echo $menu;
    ?>
</ul>

Related posts

Leave a Reply

3 comments

  1. You could use the switch_to_blog() function

    Switches the active blog until the user calls the
    restore_current_blog() function. This function is useful if you need
    to pull posts, or other information, from other blogs, you can then
    switch back after using restore_current_blog(). Using this function
    will not load plugins that only run on the blog you switch to.

    So you can edit your theme to always pull some header and footer content from the main blog, but still keep the inner content of the sub blogs.

    In you theme’s header.php and footer.php files ( and maybe some other template files** ), put switch_to_blog($id_of_main_blog) before hooks/functions that grab the site navigation and place restore_current_blog() after.

    **You will need to tweak the exact placements depending on the theme.

  2. What I would do is create a parent theme that is used on one site and a child theme of that theme for all other sites. In the parent theme’s header a you would do a regular wp_nav_menu() call to build your menu, except you wouldn’t echo it.

    Instead return the menu into a variable, pass that to update_option and then echo it. In your child theme’s header.php, replace all of that with a call to get_option(), and echo the results.

    Parent theme:

         $args = array('echo' => false);
         $menu = wp_nav_menu( $args);
         update_option('network_menu', $menu);
         echo $menu;
    

    Child theme:

         $menu = get_option('network_menu');
         echo $menu;
    

    The effect of this would be that you would have one site where you used the menu system to control the menu for every site in the network.

  3. The most flexible approach is to wrap this functionality into a plugin. If you drop this in your mu-plugins folder it’ll automatically be used on all sites. Alternatively you could just network activate it to achieve the same effect. The advantage to this approach is that you can easily turn it on or off and it makes this aspect of your site modular which can help with theme testing.

    From the Codex

    Switch the current blog to a different blog. switch_to_blog(), is
    useful if you need to pull posts or other information from other
    blogs.

    You can switch back afterwards using restore_current_blog(). Note that
    this function reverses only the last blog switching action, typically
    the most recent call to switch_to_blog(). See the example below on how
    to proceed when multiple switch_to_blog()s are used.

    Some folks say that switch_to_blog() is resource intensive but I haven’t had any issues with it during my testing.

    Andrea R wrote a plugin that approaches this issue by using WordPress caching then flushes the cache on update but the method below is more flexible.

    I’ll draw a basic example that you can build off of. It doesn’t actually add anything to the header or footer so you’ll need to hook into your themes using the plugin api.

    <?php
    /**
     * Plugin Name: Header-Footer
     * Plugin URI: example.com
     * Description: 
     * Author:
     * Author URI:
     */
    
    
    $main_site = 1;
    
    function make_menu() 
    {
        $args = array(
                'authors'      => '',
                'child_of'     => 0,
                'date_format'  => get_option('date_format'),
                'depth'        => 1,
                'echo'         => 0,
                'exclude'      => '42,311',
                'include'      => '',
                'link_after'   => '',
                'link_before'  => '',
                'post_type'    => 'page',
                'post_status'  => 'publish',
                'show_date'    => '',
                'sort_column'  => 'menu_order, post_title',
                'title_li'     => '', 
                'walker'       => ''
            );
    
            $menu = wp_list_pages( $args );
            update_option('network_menu', $menu);
            echo $menu;
    
    }
    // Test if we're on a sub-site
    if (!is_main_site())
    {
        //Switch to the main site
        switch_to_blog( $main_site );       
            make_menu();
        restore_current_blog();
    } 
    // Else use normal methods
    else
    {
        make_menu();
    }
    

    Good luck!