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 have this code that I am using on a site to check if the current user has a blog on the our wp network and as of now it is echoing the URL for both the main site and their blog. I would like to know, how can I modify the code so that it only echoes for the user's site and not the primary/main site.

<?php if(is_user_logged_in()) {
global $current_user;
  $blogs = get_blogs_of_user( $current_user->id );
     if($blogs) {
         foreach ( $blogs as $blog ) {
         echo '<li><a href="http://' . $blog->domain . $blog->path .'/upload-and-manage-documents/">My Documents</a></li>';
         echo '<li><a href="http://' . $blog->domain . $blog->path .'upload-and-manage-documents/?ptype=settings&tab=gateways">Settings</a></li>';

        }
     }
} ?>

    <?php endif; ?>

So as of now instead of 2 links, it is echoing 4. How can I fix this?

Any help is appreciated. Thanks.

share|improve this question
    
One user can own multiple sites. So you should search for sites where the user is an administrator. Does this answer solve your problem? –  toscho Dec 19 '12 at 9:51
    
Hi, thanks for the response, but Im not sure if that answers my problem. The user will not be an administrator of the main site, but they will be the admin of their own site on the network. Their site is only so that the plugin MarketPress can be used for their own store, but all the other functionality is suppose to be on the main site. How can I alter the code to only echo their site? –  mygm26 Dec 20 '12 at 1:00
    
get_user_blogs_by_role( 37, 'administrator' ); returns all the sites the user is admin. –  toscho Dec 20 '12 at 1:03
    
Ok I am going to try it. What is the number 37 for? –  mygm26 Dec 20 '12 at 5:45
    
I tried it and received this error - Fatal error: Call to undefined function get_user_blogs_by_role() in... <?php if(is_user_logged_in()) { global $current_user; $blogs = get_user_blogs_by_role( 37, 'administrator' ); if($blogs) { foreach ( $blogs as $blog ) { echo '<li><a href="http://' . $blog->domain . $blog->path .'/upload-and-manage-documents/">My Documents</a></li>'; echo '<li><a href="http://' . $blog->domain . $blog->path .'upload-and-manage-documents/?ptype=settings&tab=gateways">Settings</a></li>'; } } } ?> <?php endif; ?> –  mygm26 Dec 20 '12 at 5:48
add comment

1 Answer

up vote 0 down vote accepted

Ok I was able to eventually figure it out, what I did was exclude the main blog from the code.

<?php if(current_user_can( 'edit_posts' )) {
global $current_user;
  $blogs = get_blogs_of_user( $current_user->id );
     if($blogs) {
         foreach ( $blogs as $blog ) {
if($blog->userblog_id != 1) { 
         echo '<li><a href="http://' . $blog->domain . $blog->path .'upload-and-manage-documents/">My Documents</a></li>';
         echo '<li><a href="http://' . $blog->domain . $blog->path .'upload-and-manage-documents/?ptype=settings&tab=gateways">Settings</a></li>';
}
  }
}
}
 ?>
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.