Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Can I list all the blogs in my network on one page?

share|improve this question
add comment

3 Answers 3

up vote 4 down vote accepted

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 ?>">
                    <?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(); ?>"><?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>
share|improve this answer
    
this one didnt work. Got an error on the endforeach; –  Demilio Feb 11 '12 at 19:38
    
sorry, i have forgotten an endif, last line; now it works; have tested now. –  bueltge Feb 11 '12 at 19:51
    
Small hint to use it without a db select. Current in my projects I use this snippet: $blogs = get_blog_list( 0, 'all' ); . This get a array of data about all blogs inside the MU network. Is easier to maintenance. All data to each blog can get the the follow snipper get_blog_details( $blog['blog_id'] ). Maybe this helps as note to my answer. –  bueltge Jul 23 '13 at 6:28
add comment

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($wpdb->prepare("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; ?>"><?php echo $name;?></a>
                </li>
                <?php
            }
        }
    }
    ?></ul><?php
}
share|improve this answer
    
ahh, thank you!! is it possible to show these alphabeticly? –  Demilio Feb 9 '12 at 18:16
    
Instead of echoing them out, store them in an array using the blogs name as the key, do an array sort, then echo out each item in the array –  Tom J Nowell Feb 10 '12 at 17:40
    
Why do you need first line ($bcount = get_blog_count();)? This variable is not used anywhere. –  trejder Nov 9 '12 at 21:18
    
Indeed it isn't, perhaps that line should be removed –  Tom J Nowell Nov 10 '12 at 0:25
add comment

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.'">'.$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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.