The MU forums have moved to WordPress.org

Problem with if ( $current_blog->blog_id == 1).. (5 posts)

  1. atle_grimseid
    Member
    Posted 14 years ago #

    Sjalabais everyone!

    When I am using:

    if ( $current_blog->blog_id == 1 ) { Main Blog } else { The other blogs }

    everything works just fine, but when I'm trying to actually make this work for more than one blog, like this:

    if ( $current_blog->blog_id == 1,2,3,4,5 ) { The blogs specified } else { The other blogs }

    then i do get some problems.. It wont work for me, and I guess it's not my fault, and WPMU who does not support me doing this?

    Does anyone of you guys have any idea what I might be doing wrong, og maybe what I can do to get the same result by using another code?

    - Thanks!

    Regards
    Atle Grimseid

    (edit: I am using wpmu v. 2.7.1)

  2. Person
    Member
    Posted 14 years ago #

    I've never seen the comma used like that, so I'm pretty sure it's a PHP mistake and not WPMU.

    There are a number of ways you could go about this:
    1) Using the || (or)

    if ( $current_blog->blog_id == 1 || $current_blog->blog_id == 2 || $current_blog->blog_id == 5 ) { //The blogs specified }
    else { //The other blogs }

    2) Using elseif

    if ( $current_blog->blog_id == 1 ) { //main blog }
    elseif ( $current_blog->blog_id == 2 ) { //2nd blog }
    elseif ( $current_blog->blog_id == 3 ) { //5th blog }
    else { //The other blogs }

    3) Using an array (untested but this should work)

    $blogsspecified = array(1,2,3,4,5);
    $exist = 0;
    
    for ($i = 0; i <= (sizeof($blogsspecified) - 1); i++) {
      if ($blogspecified[$i] == $current_blog->blog_id) {
        //The blogs specified
        $exist = 1;
        break;
      }
    }
    if (!$exist) { // other blogs }
  3. tdjcbe
    Member
    Posted 14 years ago #

    I'd go a different route. I would do a check against a blog option and, if true, do what you need. For example:

    $blah_check = get_option("blah");
    
    if ($blah_check == true) {
         echo("blah, blah, blah");
    } ELSE {
         echo("blue, blue, blue");
    }
  4. cafespain
    Member
    Posted 14 years ago #

    Go for the simplest option if the ids are constant

    $blogsspecified = array(1,2,3,4,5);
    if(in_array($current_blog->blog_id, $blogsspecified)) {
    // do something
    } else {
    // do something else
    }
  5. atle_grimseid
    Member
    Posted 14 years ago #

    Thanks for all the answers. Unfortunately, I still have trouble getting this to work, but maybe you guys know what I'm doing wrong. I'm not any experienced coder, so I guess there is just a small bug in the code I have so far.

    @cafespain:
    The code you gave me is working great, but only if I am putting the Blog-IDs directly into the php-file. I am using this code in a plugin, and I want the opportunity to enter the blog-ids into the plugin options-page.

    In the plugin I am using this code:

    <textarea name="XXX" type="text" rows="1" wrap="soft" id="XXX" style="width: 95%"/><?php echo get_site_option('XXX') ?></textarea>
    
    update_site_option( "XXX", $_POST[ 'XXX' ] );
    
    $gettheblogidsfromxxx = get_site_option('XXX'):
    $blogsspecified = array( $gettheblogidsfromxxx );
    if(in_array($current_blog->blog_id, $blogsspecified)) {
    // do something
    } else {
    // do something else
    }

    The only problem is that I can't type in more than 1 blog-id in the plugin optionspage. When I type only 1 BlogID everything works fine, but when I type in more than one BlogID like 1,2,3,4,5 it just does not work at all.

    Can you guys see anything wrong with the code above?

    In advance, thanks so much for help!

About this Topic

  • Started 14 years ago by atle_grimseid
  • Latest reply from atle_grimseid