The MU forums have moved to WordPress.org

Conditional code based on blog number (11 posts)

  1. cy_borg5
    Member
    Posted 14 years ago #

    I've got multiple blogs that are basically going to look identical with the exception of one paragraph or so on the themes/my_theme/index.php front page. Rather than copy the same theme over and over again for each different blog and then edit that one page to change that one bit of text I'd like to write some sort of conditional code based on the blog number and have all of the blogs use one common theme. Could someone post a snippet of PHP code that I can put in the index.php

  2. andrea_r
    Moderator
    Posted 14 years ago #

    That page pulls in the default first post. The contents of the first post is dynamic and inserted upon blog creation. you can change that via Site Admin -> Options and not edit the theme at all.

    Use the New Blog Defaults plugin to set the theme you want as default.

  3. cy_borg5
    Member
    Posted 14 years ago #

    We've got a total misunderstanding... what you said has nothing to do with my question. I'm sorry I wasn't very clear.

    Take an old classic generic WordPress theme. I have multiple blogs using that theme. On the front page of my blog I want to put an introductory paragraph of text below the header but before the loop through the most recent posts. So I called up that theme's index.php at the following location I want to insert some code...

    get_header();
    ?>
    //I want to put some conditional code right here!
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    That conditional code is based on which blog it is. I've been told there's some sort of global variable such as $blog_id that is unique to each blog in my installation.

    What I want to know is is there such a variable available? Is there a "get_blog_id()" function somewhere?

    Let me put the question a different way. Help me write a piece of code that tells me what my blog ID number is such as this...

    <p>The unique ID number of this blog is: <?php echo get_blog_id(); ?></p>

    Except that doesn't work as there is no such function. What do I do instead? Moreover if it is a variable and not a function point me to a place in the codex or somewhere that lists all such variables available.

  4. cafespain
    Member
    Posted 14 years ago #

    global $blog_id;
    
    echo $blog_id;

    Hope that helps.

  5. cy_borg5
    Member
    Posted 14 years ago #

    duhhh... I'm a total idiot :-)

    had tried everything but forgot the...

    global $blog_id;
  6. DeannaS
    Member
    Posted 14 years ago #

    Take a look at theme options, too - if you want to be able to control this through the web admin tools and not through the back end code. Which way you do it might depend on how many blogs you're thinking about having. To do it via code, you'd need to know the blog id beforehand. To do it via theme options, you'd just have to modify the theme options (via the web admin) for each blog you create. Voila!

  7. MartyThornley
    Member
    Posted 14 years ago #

    Hi, need to correct the advice given by cafespain...

    Using global $blog_id in the wrong place (for example a plugin that is adding an action to the init area) can cause potential problems as this is messing with the defined site. It ended up causing confusion between the main site and the sub-blogs.

    By using get_current_site, you have access to the same info and it fixed my problem with the confusion between what site I was in.

    $BlogID = get_current_site()->id;

    full info here: get_current_site()

  8. MartyThornley
    Member
    Posted 14 years ago #

    I take it back. I was working in the main blog at the time so it worked, but, just like it sounds, current site is the main site, not the current blog.

    Seriously - why is there no good way to get the current blog?

    Using a global variable makes no sense. It is too easy for someone to mistakenly use that generic sounding '$blog_id' somewhere in a theme or plugin and screw everything up.

    For now, going back to

    global $blog_id;

    echo $blog_id;

    But that messes things up too at times... Why?... because it uses $blog_id.

  9. MartyThornley
    Member
    Posted 14 years ago #

    Okay...

    Found a few answers that are slightly better, but the $blog_id variables seem to go in circles.

    You can use:

    global $wpdb;
    
    $current_blog_id = $wpdb->blogid;

    Or, it appears it is also stored in the actual $GLOBALS like so...

    $current_blog_id = $GLOBALS['blog_id'];

    Most of the time, $blog_id is just being set by $GLOBALS['blog_id'] or $wpdb->id;

    I started another thread in the feature request section for a function called current_blog_id()

  10. gazouteast
    Member
    Posted 14 years ago #

    Marty, as you seem to be "the man" on this right now, would the following work as a conditional?


    $current_blog_id = $GLOBALS['blog_id'];
    if $current_blog_id =< 10 {
    do this
    } else {
    do this
    }

    (the " 10 " is just a random for the example)

    There's lots of ways I could think to use this in terms of rewarding early joiners and so on.

    Gaz

  11. MartyThornley
    Member
    Posted 14 years ago #

    gazouteast -

    That definitely sounds right. But as with anything only testing it out will tell.

    One thing - you're missing your parenthesis () around the if statement. I always do that too :)

    The problem is, all these terms are generic and yet are used pretty extensively by WordPress as global variables, making it very easy to mess things up.

    I even think the $post variable is strange. It is called from a function and until you know the way WordPress works, it seems like it come out of nowhere.

    But getting the info from $GLOBALS['blog_id'] seems to be the most reliable, that way even if some plugin or theme has redefined $blog_id you can get the right info.

About this Topic

  • Started 14 years ago by cy_borg5
  • Latest reply from MartyThornley