Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress Multisite: List of all blogs
<?php
function projects_menu( $atts = array() ) {
global $wpdb, $blog_id;
$a = shortcode_atts( array(
'link_self' => false,
'format' => 'ul', // ol, dl... TODO
'slogan' => false, // TODO
'sort_by' => 'title', // or ID TODO
'order' => 'ASC', // or DESC TODO
'show_posts_number' => false // TODO
), $atts );
// TODO switch to https://developer.wordpress.org/reference/functions/get_sites/
$blogs = $wpdb->get_results("
SELECT blog_id
FROM {$wpdb->blogs}
WHERE site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0'
");
$sites = array();
foreach ($blogs as $blog) {
$sites[$blog->blog_id] = get_blog_option($blog->blog_id, 'blogname');
}
natsort($sites);
$return = '<ul>';
foreach ($sites as $id => $title) {
if ($id == $blog_id && !$a['link_self']) continue;
$return .= '<li><a href="' . get_home_url($id) . '">' . $title . '</a></li>';
}
$return .= '</ul>';
return $return;
}
add_shortcode( 'site-list', 'projects_menu' );
@RavanH

This comment has been minimized.

Copy link
Owner Author

RavanH commented Sep 20, 2017

fix shortcode echoing (must return); prepare for some shortcode attributes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.