-1

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.

5
  • One user can own multiple sites. So you should search for sites where the user is an administrator. Does this answer solve your problem?
    – fuxia
    Dec 19, 2012 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?
    – 730wavy
    Dec 20, 2012 at 1:00
  • get_user_blogs_by_role( 37, 'administrator' ); returns all the sites the user is admin.
    – fuxia
    Dec 20, 2012 at 1:03
  • Ok I am going to try it. What is the number 37 for?
    – 730wavy
    Dec 20, 2012 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; ?>
    – 730wavy
    Dec 20, 2012 at 5:48

1 Answer 1

0

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>';
}
  }
}
}
 ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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