WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get the current site's/blog's ID? (9 posts)

  1. carl-johan
    Member
    Posted 3 years ago #

    Sorry if this issue has already been addressed and answered, but I can't find it anywhere.

    How do I/Can I fetch the current site's ID?
    (Or blogs? sorry, not sure which terminology is being used)

    I would like to set up a check whether you are currently viewing the main site or a sub site, something like;

    if ( blog_ID = 1 ) {
    You are on the main site
    } else {
    You are on a sub site
    }

    I found this code in the codex;

    <?php
    $blog_details = get_blog_details(1);
    echo 'Blog '.$blog_details->blog_id.' is called '.$blog_details->blogname.'.';
    ?>

    So what I need is to get the ID and insert it where that code suggests a check for Blog ID 1's details.

    Thanks in advance!

    / Carl-Johan

  2. Person
    Member
    Posted 3 years ago #

    <?php
    global $blog_id;
    if ($blog_id == 1) {
       //do something
    } else {
       //do something else
    }
    ?>
  3. carl-johan
    Member
    Posted 3 years ago #

    A, cheers man!

    Now I feel really embarrassed :)

    Is there a list of the globals anywhere that I've missed? I haven't been using MU at all, and I'm building my first MS.

    Thanks again!

  4. Person
    Member
    Posted 3 years ago #

    This should probably help:
    http://codex.wordpress.org/WPMU_Global_Variables

    If you need to retrieve multiple variables all at once, use the $current_site or $current_blog objects. For example:

    global $current_site;
    if($current_site->id == 1) {
       echo $current_site->path;
    }
  5. carl-johan
    Member
    Posted 3 years ago #

    Yes, those will come real handy I'm sure. Thanks again!

  6. jumust
    Member
    Posted 3 years ago #

    Hi,
    I'm trying to use the code above

    <?php
    global $blog_id;
    if ($blog_id == 4) {
       //First blog links
    } else {
       //Default links <a href="http:\\example.com>example</a>
    }
    ?>

    Does it work in WP3 as well?

    I would like to display different links on the top for each blog in my Network, but it's not displaying anything.

    Please help!!! Thanks

  7. David Sader
    Member
    Posted 3 years ago #

    If you have exactly what you've pasted, jumust, you actually need to include some links after the comment

    //First blog links
    echo '<a href="http://example.com>example</a>';

    and so on.

  8. jumust
    Member
    Posted 3 years ago #

    Thanks David but I can't display what I want.
    Here it is what I have:

    <div id="header" class="col-full">
    
     <div class="toplinks">
    <?php
    global $blog_id;
    if ($blog_id == 4) {
       //First blog links
    echo '<a href="http://example.com>custom link</a>';
    } else {
       //Default links <a href="http:\\example.com>dafault</a>
    }
    ?>
    </div>

    So "toplinks" tag goes above the logo, but I get this: see here
    I assume it displays the blog title and tagline because of global $blog_id; but it doesn't display any links above the logo within the "toplinks" tag.

    Any helps is much appreciate

  9. jumust
    Member
    Posted 3 years ago #

    Hi,
    I think I had just missed " after example.com and also the \\ after the http: are definitely wrong.
    Anyway thanks so much

Topic Closed

This topic has been closed to new replies.

About this Topic