2

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.

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>
3
  • How dynamic shared parts need to be? Would they need to actually access data from main blog or would they be pretty static?
    – Rarst
    Mar 28, 2014 at 18:20
  • The shared parts are only changable by the super admin and the main blog. The subblog wouldn't need to change anything from the main blog. The subblogs should just be able to edit their content-area where there own sites and posts will be listed. Mar 29, 2014 at 7:50
  • Can you tell me the theme you are using at the moment? Apr 22, 2014 at 13:55

3 Answers 3

0
+100

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
  • how about the footer? is this also possible without using wp_nav_menu()? Apr 19, 2014 at 9:42
  • If don't change the footer.php inside the child theme, then will use the sub blogs always the default footer from the parent theme, with all content. But if you have dynamicly content, like wp_nav_menu() inside the footer.php, then is it important, that you add a footer.php, there get the option from the network, like get_option('network_menu');.
    – bueltge
    Apr 19, 2014 at 9:48
  • @buelte I updated my question. I'm not using wp_nav_menu() but wp_list_pages on my theme. The code above doesn't work on the child-theme. On my child-theme there is no menu at all. Apr 19, 2014 at 9:54
0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.