0

In a site's header.php which is in a child theme shared by the root and 4 other sites on a network (using MU configured in directories) I need to identify which site/sub-site is being shown and subsequently look for other conditionals.

I've seen mention of get_blog_id_from_url, get_current_site and blog_id as potential methods, some more reliable than others. Which is the better approach?

I basically need the following (oversimplified):

If site 2, then 
- if is_single() {echo some code}
- elsif is_template() {echo some code}
elsif site 3, then
- same as above but with different output

I'm essentially coming up with a rudimentary way to implement conditional menus. Plugins I have tried haven't worked as they were supposed to and I don't want to rely on a plugin for such a vital function.

If someone could lend a hand to figuring this out, it would be much appreciated.

0

You should use get_current_blog_id() if you need just the id and get_blog_details() if you need more fields.

Be aware that get_current_site in fact gives you information about the network of sites in a WP_Network object. Yeah, the name is kind of confusing, as noted in the Codex but not yet on developer docs:

Note: get_current_site() was inherited from MU, and is named based on the old terminology which spoke of multiple "blogs" on a "site". We now speak of multiple "sites" on a "network" instead, but some of the old terminology still lives on in some function names. This function returns information about the current network. To get information about the current site on the network, see get_current_blog_id() and get_blog_details().

It would be something like this.

// get current blog id
$blog_id = get_current_blog_id();
if ( 2 === $blog_id){
    // if site id is 2
    //middle part for this case
} else {
    //if is not
    //middle part for this case
}
  • Thanks. I just stumbled on get_current_blog_id() today and thought it might do the trick, especially since I'm dealing with a limited number of sites, not expecting to grow much if at all. Especially since it seems I can pull domain / path as well. And no, I don't need any more fields, just identifying each site is enough. Now I just need to pretend I know more about php than I do to make it all work. As it stands, I have an if statement handling the middle part (if is_single, etc) but I haven't worked out what exactly the parent if statement using get_current_blog_id() will look like. – yantat Oct 26 '17 at 1:15
  • Added a general example – Celso Bessa Oct 26 '17 at 13:32
  • This got me going in the right direction, thank you. – yantat Nov 12 '17 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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