Skip to content

Instantly share code, notes, and snippets.

@ryanl4321
Last active December 23, 2015 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanl4321/bd49ab55975afec80c79 to your computer and use it in GitHub Desktop.
Save ryanl4321/bd49ab55975afec80c79 to your computer and use it in GitHub Desktop.
WordPress Multisite: List of all blogs
<?php
// This code was used to generate a menu showing all blogs on a WordPress
// Multisite installation.
// The shortcode [bloglist] was used in the homepage of the main blog to link to
// all the other blogs.
// Output a single menu item
function bloglist_entry($id, $title, $link_self){
global $blog_id;
if ($id == $blog_id && !$link_self){ return; }
$str = '<li>';
if ($id == $blog_id){ $str .= '<strong>'; }
//blog home url
$url = rtrim(get_home_url($id),'/').'/';
$str .= '<a href="' . $url . '">' . $title . '</a>';
if ($id == $blog_id){ $str .= '</strong>'; }
$str .= '</li>';
return $str;
}
// Output the whole menu
// If $link_self is false, skip the current site - defaults to true
// used to display the menu on the homepage
function bloglist_shortcode($atts){
global $wpdb;
$link_self = isset($atts['link_self']) ? $atts['link_self'] : true;
$str = $atts['title'] != '' ? '<a href="'.$atts['href'].'">'.$atts['title'].'</a>' : '';
$str .= '<ul class="sub-menu">';
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0'".
($link_self ? "":"AND domain != '".$_SERVER['SERVER_NAME']."'")
);
$sites = array();
foreach($blogs as $blog){
$sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'blogname');
}
natsort($sites);
foreach($sites as $blog_id => $blog_title){
$str .= bloglist_entry($blog_id, $blog_title, $link_self);
}
$str .= '</ul>';
return $str;
}
// Adds a [bloglist] shortcode, so I can embed the menu wherever you want.
add_shortcode('bloglist','bloglist_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment