0

I am using this function that gives me a shortcode I can paste anywhere on the site. It lists all blogs in the multisite network a user has, but I would like to hide the main site from the list. Can someone help to adjust the snippet?

-Extra question. - is it possible to show blogs website preview like Facebook?

function show_blog_list() {
 
    $user_id = get_current_user_id();
    if ($user_id == 0) {
        echo 'You are not logged in.';
    } else {
        echo '<h2>Here is your blog list</h2>';
        $user_blogs = get_blogs_of_user( $user_id );
        foreach ($user_blogs AS $user_blog) {
            echo '<li><a href="'.$user_blog->siteurl.'">'.$user_blog->blogname.'</a></li>';
        }
        echo '</ul>';
    }
 
}
add_shortcode( 'bloglist', 'show_blog_list' );

1 Answer 1

1

I've never used it, but it looks like get_main_site_id is what you want to figure that out, so therefore your code might look like:

$mainSiteId = get_main_site_id();
foreach ($user_blogs AS $user_blog) {
    if ($userblog->site_id != $mainSiteId) {
        echo '<li><a href="'.$user_blog->siteurl.'">'.$user_blog->blogname.'</a></li>';
    }
}
4
  • Sorry, I am totally new to php. Is this something I add to my existing code above? What I am looking for is to hide the main blog from the list.
    – Sanfe
    Jul 31, 2020 at 7:30
  • This is a modification to the similar looking part in your existing code. You just need to add/change the bits that I added
    – mozboz
    Jul 31, 2020 at 9:34
  • Thanks! Do you know how it can be adjusted to hide the main site?
    – Sanfe
    Aug 3, 2020 at 16:23
  • @Sanfe That's what the code in my answer does. Did you try it? You may want to read the answer above as the code should be understandable.
    – mozboz
    Aug 3, 2020 at 16:25

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.