Exclude main blog from get_blogs_of_user

I use WordPress Multisite and in the sidebar i have a box that shows a list of blogs the logged in user is a member of.

I’m looking for a way to exclude the mainblog from this list.
The main blog ID is 1

Read More

Here is parts of the code I use:

<?php // Gets user-info ?>
<?php global $current_user; get_currentuserinfo(); 
     $user_info = get_userdata(1); 
     $user_id = $current_user->ID; 
?> 

<?php // start the loop ?>
<?php $user_blogs = get_blogs_of_user( $user_id ); echo '<div>'; foreach ($user_blogs AS $user_blog) { echo '';?>

<?php // lists user blogs ?>
<?php echo ''.$user_blog->blogname.' '; ?>

Can anyone help me? 🙂

Related posts

Leave a Reply

1 comment

  1. If you drop this line in your code, you will see all the properties of $user_blogs.
    echo '<pre>'.print_r($user_blogs,true).'</pre>';

    One of them is userblog_id, so you just have to check against it before echoing the blogname.

    <?php 
    $user_blogs = get_blogs_of_user( $user_id ); 
    if (!$user_blogs) {
        echo 'no blogs';
    } else {
        echo '<div><ul>'; 
        foreach ( $user_blogs as $user_blog ) {
            if ( $user_blog->userblog_id != get_current_blog_id() ) { 
                echo '<li>' . $user_blog->blogname . '</li>';
            }
        }
        echo '</ul></div>'; 
    }