SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2013
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple IF OR statement

    Hey all,

    I have about 12 custom post types in wordpress and i am looking for an IF statement that is efficient and speedy.

    Basically, if the user is on a single post of 5 specific custom post types i would like to echo a specific blurb of text.

    The code should go on single.php I guess and it should be something like:

    Code:
    IF post_type = os_red OR post_type = os_blue OR post_type = green etc.. then
    
    echo...
    
    END IF
    I would appreciate an efficient syntax!

    many thanks in advance,
    Andy

  2. #2
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,182
    Mentioned
    66 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by neodjandre View Post
    Hey all,

    I have about 12 custom post types in wordpress and i am looking for an IF statement that is efficient and speedy.

    Basically, if the user is on a single post of 5 specific custom post types i would like to echo a specific blurb of text.

    The code should go on single.php I guess and it should be something like:

    Code:
    IF post_type = os_red OR post_type = os_blue OR post_type = green etc.. then
    
    echo...
    
    END IF
    I would appreciate an efficient syntax!

    many thanks in advance,
    Andy
    Have a look here: http://www.php.net/manual/en/languag...comparison.php and here: http://php.net/manual/en/language.operators.logical.php

    Code PHP:
    if($post_type == "os_red"||$post_type == "os_blue"||$post_type == "green") {
         echo "foo";
    }

    You can also use things such as in_array(): http://php.net/manual/en/function.in-array.php to write less code, if it fits your requirements.

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2011
    Posts
    55
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello neodjandre,

    I would recommend to use a switch case instead of if or statement. It will be easy to use and modify.

    switch($post_type)
    {
    case 'os_red': echo 'os_red';
    break;

    case 'os_blue': echo 'os_blue';
    break;

    case 'os_green': echo 'os_green';
    break;

    ..........
    }
    PHP Programming Itís more than just writing code

  4. #4
    SitePoint Zealot wh33t's Avatar
    Join Date
    Aug 2011
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by neodjandre View Post
    Hey all,

    I have about 12 custom post types in wordpress and i am looking for an IF statement that is efficient and speedy.

    Basically, if the user is on a single post of 5 specific custom post types i would like to echo a specific blurb of text.

    The code should go on single.php I guess and it should be something like:

    Code:
    IF post_type = os_red OR post_type = os_blue OR post_type = green etc.. then
    
    echo...
    
    END IF
    I would appreciate an efficient syntax!

    many thanks in advance,
    Andy
    If you only need to use "if" as in one value out of a bunch of values I would use a "switch" statement like someone else mentioned, but I do not know how to do an "OR" statement with a switch, so if you need to use OR then I think you are destined to use "if".

    Don't forget you can do some pretty fancy "if" logic when you start utilizing brackets.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I'd say that using the in_array() function would definitely be the best solution here, however to shorten @jimmybrion's approach of the switch statement, we can stack multiple case statements to validate any number of values:
    PHP Code:
    <?php

    switch($val) {
        case 
    'val1':
        case 
    'val2':
        case 
    'val3':
        case 
    'val4':
        case 
    'val5':
            
    //contains 1 of 5 potential values
        
    break;
    }

  6. #6
    SitePoint Zealot wh33t's Avatar
    Join Date
    Aug 2011
    Posts
    154
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by tpunt View Post
    I'd say that using the in_array() function would definitely be the best solution here, however to shorten @jimmybrion's approach of the switch statement, we can stack multiple case statements to validate any number of values:
    PHP Code:
    <?php

    switch($val) {
        case 
    'val1':
        case 
    'val2':
        case 
    'val3':
        case 
    'val4':
        case 
    'val5':
            
    //contains 1 of 5 potential values
        
    break;
    }
    Very neat, I didn't know you could do that.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •