patrick
patrick

Reputation: 657

php multiple if statements?

This is going to seem like a noob question, sorry. I can't get my brain working this morning.

I am trying to perform multiple if statements, but they are not behaving properly. It appears to be always loading the least Template after it finds the one that it is looking for. What is the best way to do something like this:

$post = $wp_query->post;
if ( in_category('7') ) {include(TEMPLATEPATH . '/post-experts.php');}
if ( in_category('6') ) {include(TEMPLATEPATH . '/post-radio.php');}
if ( in_category('5') ) {include(TEMPLATEPATH . '/post-lifestyle.php');}
else {include(TEMPLATEPATH . '/singleorigional.php');
}

example

Upvotes: 4

Views: 52220

Answers (5)

cmprogram
cmprogram

Reputation: 1884

For efficiency, you're best to use a switch statement, and then to catch those you have not found in your cases, you can use a default.

switch(in_category){ //Begin switch statement.
case '7': //Check if it equals 7
include(TEMPLATEPATH . '/post-experts.php'); //Include our PHP code
break; //End this current condition.
case '6': //Check if it equals 6
include(TEMPLATEPATH . '/post-radio.php'); //Include our PHP code
break; //End this current condition.
case '5': //Check if it equals 5
include(TEMPLATEPATH . '/post-lifestyle.php'); //Include our PHP code
break; //End this current condition.

default: //If none of the above cases are found, do this.
include(TEMPLATEPATH . '/singleorigional.php'); //Include our PHP code
break; //End this current condition.   
}

Edit: I decided to return to this at a later date, to better explain why this is better.

An if else combination implies an order. E.g.

if(thingy == "thing1"){
//Do one thing
}
elseif(thingy == "thing2"){
//Do another thing
} 
elseif(thingy == "thing3"){
//Do a different thing
}
else{
//Catch anything
}

With this, it means it will check the first condition, if thing == thing1, if not, check if it equals the next condition which is thing == thing2 and so on. If you're generally always expecting thing1, then this might be okay, because you're just catching a few other things. However, realistically it's inefficient to check all possible conditions before you reach the solution you need.

Instead, by writing the equivalent switch statement:

switch(thingy){
case "thing1":
//Do one thing
break;
case "thing2":
//Do another thing
break;
case "thing3":
//Do a different thing
break;
default:
//Catch anything
break; //Break is not needed if default is the final case.
}

What this does instead, is grab the answer first, e.g. thing == "thing3", and then it'll skip the other cases that are irrelevant, and instead only do what it needs to do. It does not use an order, instead it works a bit like pointing to the correct answer. So it doesn't matter if your actual answer is the first case, or the hundredth, it only does what is relevant.

So to summarise: If you use a switch, and your answered case is the hundredth case, it'll point to what to do after that switch(answer) is found, if you were to use ifelse and your answer was the 100th variation of the ifelse, it would have to iterate through 99 other pointless checks before doing what it needs to do.

Upvotes: 10

nyson
nyson

Reputation: 1055

As @thantos said, include else if statements.

EDIT: WordPress tells me it needs a second argument to the in_category function, namely the $post variable; I edited my response to match that.

Example:

$post = $wp_query->post;
if (in_category('7', $post)) {
    include(TEMPLATEPATH . '/post-experts.php');
}
else if (in_category('6', $post)) {
    include(TEMPLATEPATH . '/post-radio.php');
}
else if (in_category('5', $post)) {
    include(TEMPLATEPATH . '/post-lifestyle.php');
}
else {
    include(TEMPLATEPATH . '/singleorigional.php');
}

Upvotes: 0

bacardnumberone
bacardnumberone

Reputation: 51

i think you the problem is that the If statements are independent. Either if you have array with categories try use switch statement or if you have only in_category function which a think returns boolean then use elseif statement e.g.:

if (in_category(7)){...}
elseif (in_category(6)){...}
elseif (in_category(5)){...}
else {
...
}

Upvotes: 4

Dave
Dave

Reputation: 29121

My assumption is that whatever it is you're looking for "in_category" can be found in more than one category - hence not one long if block. Try this:

Condensed version:

$post = $wp_query->post;
$found = false;
if ( in_category('7') ) { include(TEMPLATEPATH . '/post-experts.php'); $found = true; }
if ( in_category('6') ) {include(TEMPLATEPATH . '/post-radio.php'); $found = true; }
if ( in_category('5') ) {include(TEMPLATEPATH . '/post-lifestyle.php'); $found = true; }
if(!$found) include(TEMPLATEPATH . '/singleorigional.php');

More easy to read/understand version:

$post = $wp_query->post;
$found = false;
if ( in_category('7') ) {
    include(TEMPLATEPATH . '/post-experts.php');
    $found = true;
}
if ( in_category('6') ) {
    include(TEMPLATEPATH . '/post-radio.php');
    $found = true;
}
if ( in_category('5') ) {
    include(TEMPLATEPATH . '/post-lifestyle.php');
    $found = true;
}
if(!$found) include(TEMPLATEPATH . '/singleorigional.php');

OR - if it can only be found in one category:

$post = $wp_query->post;
if ( in_category('7') ) {
    include(TEMPLATEPATH . '/post-experts.php');
} else if ( in_category('6') ) {
    include(TEMPLATEPATH . '/post-radio.php');
} else if ( in_category('5') ) {
    include(TEMPLATEPATH . '/post-lifestyle.php');
} else {
    include(TEMPLATEPATH . '/singleorigional.php');
}

Upvotes: 2

Sam Sussman
Sam Sussman

Reputation: 1045

You most likely want to do else if for the 2nd and 3nd ifs or have a way to know if none of the are true, do the else statement

Upvotes: 9

Related Questions