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:
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
neodjandre:
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:
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/language.operators.comparison.php and here: http://php.net/manual/en/language.operators.logical.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.
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;
…
}
wh33t
February 22, 2013, 12:45am
4
neodjandre:
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:
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.
tpunt
February 22, 2013, 11:26am
5
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
switch($val) {
case 'val1':
case 'val2':
case 'val3':
case 'val4':
case 'val5':
//contains 1 of 5 potential values
break;
}
wh33t
February 22, 2013, 7:59pm
6
tpunt:
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
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.