Skip to content

Instantly share code, notes, and snippets.

@sosukeinu
Last active August 29, 2015 14:18
Show Gist options
  • Save sosukeinu/0c1fe250b378954421de to your computer and use it in GitHub Desktop.
Save sosukeinu/0c1fe250b378954421de to your computer and use it in GitHub Desktop.
<?php
// This code was used to generate a menu showing all blogs on a WordPress
// Multisite installation.
// projects_menu() was used in the template to show a dropdown menu on all the
// sites.
// 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 projects_menu_entry($id, $title, $link_self)
{
global $blog_id;
if ($id == $blog_id && !$link_self)
return;
echo '<li>';
if ($id == $blog_id)
echo '<strong>';
$url = get_home_url($id);
if (substr($url, -1) != '/') {
// Note: I added a "/" to the end of the URL because WordPress
// wasn't doing that automatically in v3.0.4. YMMV.
$url .= '/';
}
echo '<a href="' . $url . '">' . $title . '</a>';
if ($id == $blog_id)
echo '</strong>';
echo '</li>';
}
// Output the whole menu
// If $link_self is false, skip the current site -
// used to display the menu on the homepage
function projects_menu($link_self = true)
{
global $wpdb;
echo '<ul>';
projects_menu_entry(1, 'Home', $link_self);
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0'
AND blog_id != 1
");
$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) {
projects_menu_entry($blog_id, $blog_title, $link_self);
}
echo '</ul>';
}
// Adds a [bloglist] shortcode, so I can embed the menu into the static homepage.
// Note: I originally put it directly into the template, but that didn't work
// with WPtouch.
add_shortcode('bloglist', function($atts)
{
projects_menu(false);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment