Build A WordPress Network Navigation Menu

WordPress 3.0 has allowed users to host multiple independent websites with just one feature called Multisite. It also lets you share settings, content, and code between websites within your network.

Kevin Leary has shared some of the hacks he did to perform the aforementioned abilities in one of his articles; one of them involves building a network navigation menu. Below is a snippet of the code:

Simply paste this in your theme’s functions.php file.

/** 

* Build a list of all websites in a network

*/

function wp_list_sites( $expires = 7200 ) {

if( !is_multisite() ) return false;

// Because the get_blog_list() function is currently flagged as deprecated

// due to the potential for high consumption of resources, we’ll use

// $wpdb to roll out our own SQL query instead. Because the query can be

// memory-intensive, we’ll store the results using the Transients API

if ( false === ( $site_list = get_transient( ‘multisite_site_list’ ) ) ) {

global $wpdb;

$site_list = $wpdb->get_results( $wpdb->prepare(‘SELECT * FROM wp_blogs ORDER BY blog_id’) );

// Set the Transient cache to expire every two hours

set_site_transient( ‘multisite_site_list’, $site_list, $expires );

}

$current_site_url = get_site_url( get_current_blog_id() );

$html = ‘

<ul id=”network-menu”>’ . “\n”;

foreach ( $site_list as $site ) {

switch_to_blog( $site->blog_id );

$class = ( home_url() == $current_site_url ) ? ” : ”;

$html .= “\t” . ‘

<li id=”site-’ . $site->blog_id . ‘” ‘=”" .=”" $class=”"><a href=”‘ . home_url() . ‘”>’ . get_bloginfo(‘name’) . ‘</a></li>

‘ . “\n”;

restore_current_blog();

}

$html .= ‘</ul>

<!-// end #network-menu ->’ . “\n\n”;

return $html;

}

You can use it in your theme by calling its wp_list_sites() function.

<?php 

// Multisite Network Menu

$network_menu = wp_list_sites();

if( $network_menu ):

?>

<div id=”network-menu”>

<?php echo $network_menu; ?>

</div>

<!-// end #network-menu ->

<?php endif; ?>

Cool, huh? Find out more about the rest of his WordPress hacks here!

Incoming search terms for the article:

Related Posts

eBag Toolkit

Creating a Psychedelic Art Effect in Your Portraits

Inspirational Photo Retouches By Cristian Girotto

Create Your Own Sticker Design Via Photoshop

1 Comment

  1. Create Website

    02.07.2012

    Thanks for those tips Keith, just what I was looking for.