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
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.
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>
You could use the
switch_to_blog()
functionSo 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 placerestore_current_blog()
after.**You will need to tweak the exact placements depending on the theme.
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:
Child theme:
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.
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
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.
Good luck!