Post

Another Random Media & Technology Post

WP MultiSite List of Sites with Links

WP MultiSite List of Sites with Links

When I was working on creating a multi-site installation to show a number of demo themes that I offer, I needed to show a list of all of the sites and list them in alphabetic order with links to each one. There are plugins that have the same functionality but I wanted to put the list of links into a slide out tab and make it available on the whole network. This little bit of code works just fine.  It can also be placed in a WordPress Template file and displayed on a single page:

<?php


// Query for getting blogs
$sites = wp_get_sites();
unset($sites[0]);
foreach ( $sites as $i => $site ) {
switch_to_blog( $site[ 'blog_id' ] );
$sites[ $i ][ 'name' ] = get_bloginfo();
restore_current_blog();
}
uasort( $sites, function( $site_a, $site_b ) {
return strcasecmp( $site_a[ 'name' ], $site_b[ 'name' ] );
});
foreach ( (array) $sites as $details ) {$blog_list[ $details['blog_id'] ] = $details;}
unset( $sites);


//var_dump($sites);
$sites = $blog_list;
if (is_array( $sites ) ){
echo '<ul>';
$array= array();


// reorder
$array= array_slice( $sites, 0, count( $sites ) );
for($i=0;$i<count($array);$i++){
// get data for each id
$blog = get_blog_details( $array[$i]['blog_id'] );
// print it
echo '<li><a href="'.$blog->siteurl.'">'.$blog->blogname.'</a></li>';
}
echo '</ul>';
}

?>

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.