Global page navigation accross WordPress MU blogs
January 29, 2009
Categories: Web & Software Development
Tags: Wordpress, WordPress MU
Here’s the scenario. You have a WordPress MU site with multiple blogs, but for whatever reason, you want every blog to have the same main navigation and pages. Your main site/blog hosts all of your pages. So how do you go about creating a WordPress MU Theme that will use the navigation and pages from your main site/blog accross all of your blogs? Well, I’m here to tell you how I did it.
The WordPress Codex has a function called ‘wp_list_pages.’ What I wanted to do is create a new function called ‘wp_list_main_pages.’ So I simply searched my WordPress install and copied the ‘get_pages’ and ‘wp_list_pages’ functions and pasted them into my theme’s ‘functions.php’ file. From here I made a few edits to these functions. To start with, I renamed them ‘wp_list_main_pages’ and ‘get_main_pages.’
Below is the final code I’m using to display the main navigation accross blogs. I made notes in bold to help. Hope this is helpful to you Googling coders.
// You can use this function instead of 'wp_list_pages' in your theme
function wp_list_main_pages($args = '') {
$defaults = array(
'depth' => 0, 'show_date' => '',
'date_format' => get_option('date_format'),
'child_of' => 0, 'exclude' => '',
'title_li' => __('Pages'), 'echo' => 1,
'authors' => '', 'sort_column' => 'menu_order, post_title'
);
$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );
$output = '';
$current_page = 0;
// sanitize, mostly to keep spaces out
$r['exclude'] = preg_replace('[^0-9,]', '', $r['exclude']);
// Allow plugins to filter an array of excluded pages
$r['exclude'] = implode(',', apply_filters('wp_list_pages_excludes', explode(',', $r['exclude'])));
// Query pages.
$r['hierarchical'] = 0;
// Right here we call our new 'get_main_pages' function
$pages = get_main_pages($r);
if ( !empty($pages) ) {
if ( $r['title_li'] )
$output .= '<li class="pagenav">' . $r['title_li'] . '<ul>';
global $wp_query;
if ( is_page() || $wp_query->is_posts_page )
$current_page = $wp_query->get_queried_object_id();
$output .= walk_page_tree($pages, $r['depth'], $current_page, $r);
if ( $r['title_li'] )
$output .= '</ul></li>';
}
$output = apply_filters('wp_list_pages', $output);
// This line is sloppy and needs improvement.
// You have to remove the name of the blog your currently on from you global navigation.
// I'm doing this the simplest but least scalable way here.
$output = str_replace("pressroom/", "", $output);
if ( $r['echo'] )
echo $output;
else
return $output;
}
// This is essentially a private function
function &get_main_pages($args = '') {
global $wpdb;
// This is the magic line.
// Now when the SQL runs to pull your navigation pages, it'll use your main blogs ID.
$wpdb->set_blog_id(1);
// Notice here I call the original get_pages function and return the results
$pages = get_pages($args);
return $pages;
}
