Can I list all the blogs in my network on one page?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
yes, small source for an template.
<ul class='postlist no-mp'>
<?php
/**
* Old version, change on 07/23/2013
*
$blogs = $wpdb->get_results(
"SELECT blog_id,path FROM {$wpdb->blogs}
WHERE blog_id != {$wpdb->blogid}
AND site_id = '{$wpdb->siteid}'
AND spam = '0'
AND deleted = '0'
AND archived = '0'
order by blog_id", ARRAY_A
);
*/
// get all blogs
$blogs = get_blog_list( 0, 'all' );
if ( 0 < count( $blogs ) ) :
foreach( $blogs as $blog ) :
switch_to_blog( $blog[ 'blog_id' ] );
if ( get_theme_mod( 'show_in_home', 'on' ) !== 'on' ) {
continue;
}
$description = get_bloginfo( 'description' );
$blog_details = get_blog_details( $blog[ 'blog_id' ] );
?>
<li class="no-mp">
<h2 class="no-mp blog_title">
<a href="<?php echo $blog_details->path ?>" rel="nofollow noreferrer noopener">
<?php echo $blog_details->blogname; ?>
</a>
</h2>
<div class="blog_description">
<?php echo $description; ?>
</div>
<?php
query_posts( 'showposts=5' );
if ( have_posts() ) :
while( have_posts() ) :
the_post();
?>
<div class="blog_post">
<div class="post_title">
<a href="<?php the_permalink(); ?>" rel="nofollow noreferrer noopener"><?php the_title(); ?></a>
</div>
<div class="post_excerpt">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php endif;
restore_current_blog();
?>
</li>
<?php endforeach;
endif; ?>
</ul>
Method 2
This will print out an unordered list of all public sites in a multisite network:
// $bcount = get_blog_count();
global $wpdb;
$blogs = $wpdb->get_results("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'");
if(!empty($blogs)){
?><ul class="menu"><?php
foreach($blogs as $blog){
$details = get_blog_details($blog->blog_id);
if($details != false){
$addr = $details->siteurl;
$name = $details->blogname;
if(!(($blog->blog_id == 1)&&($show_main != 1))){
?>
<li class="menu-item<?php if($counter == get_current_blog_id()){ echo ' current-menu-item';}?>">
<a href="<?php echo $addr; ?>" rel="nofollow noreferrer noopener"><?php echo $name;?></a>
</li>
<?php
}
}
}
?></ul><?php
}
Method 3
This is a solution written basing on Tom J Nowell idea and answer. It prints out sorted list of all sites in WordPress Multisite installation, as simple line (separated with pipe).
To get this solution running, edit your currently selected theme and select shortcodes.php from right sidebar. Near the end of this file, before first occurence of add_shortcode call add following function:
function theme_list_all_network_sites()
{
global $wpdb;
$result = '';
$sites = array();
$blogs = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'"));
if(!empty($blogs))
{
foreach($blogs as $blog)
{
$details = get_blog_details($blog->blog_id);
if($details != false)
{
$url = $details->siteurl;
$name = $details->blogname;
if(!(($blog->blog_id == 1) && ($show_main != 1)))
{
$sites[$name] = $url;
}
}
}
ksort($sites);
$count = count($sites);
$current = 1;
foreach($sites as $name=>$url)
{
$result.= '<a href="'.$url.'" rel="nofollow noreferrer noopener">'.$name.'</a>';
$result.= ($current == $count) ? "n" : ' | ';
++$current;
}
}
return $result;
}
Then scroll down to the end of file and after last occurence of add_shortcode add:
add_shortcode('network_list', 'theme_list_all_network_sites');
Click Update File to save your changes.
Now, whenever anyone use [network_list] shortcode to in post, page or theme element, a list of network sites will be printed in place of that shortcode.
Method 4
Since WordPress 4.6.0, I offer you a more modern way to “list” (or more) :
<?php $sites_q = new WP_Site_Query( [
'site__not_in' => get_main_site_for_network(),
] );
$sites_for_network = $sites_q->get_sites();
if ( ! empty( $sites_for_network ) ) {
$original_site_id = get_current_blog_id();
foreach ( $sites_for_network as $site ) {
/** @var $site WP_Site */
switch_to_blog( $site->blog_id );
// Do stuff with the site
}
switch_to_blog( $original_site_id );
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0