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.

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

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? :)

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

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 != BLOG_ID_CURRENT_SITE ) { 
            echo '<li>' . $user_blog->blogname . '</li>';
        }
    }
    echo '</ul></div>'; 
}
share|improve this answer
    
it does list blogs without id 1, but it also loops blog nr 1. So if I want to wrap a <li>your code</li> around it it leaves an empty list in the start. Shouldnt the id 1 be excluded in the loop code? –  Demilio May 18 '12 at 9:03
    
You are only gonna print an empty list item if you want to. I extended my example to make this clearer. You could remove the array item where the blog id equal 1 before doing the foreach, but I don't think it's worth the trouble. –  brasofilo May 18 '12 at 9:49
    
you did it my friend. Missing a ' in the end </li> there, but perfect!! this was really helpful to me :) thanks! –  Demilio May 18 '12 at 13:55
    
ok, I have another challange. Is it possible to as an "else" if the user has no blogs?. It would be great to ass a text i.e. "You dont have any blogs yet. Click here to create one" –  Demilio May 18 '12 at 14:47
    
Added to the code, have phun..! –  brasofilo May 18 '12 at 16:36
show 1 more 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.