-1

I want to set a conditional in header.php to wrap all of my templates, except front-page.php and single-device.php, in a bootstrap container element.

Writing one conditional achives what I want

if( !is_singular('device') ) {
   echo '<div class="container">';
}

However, when including two conditionals, only the first conditional applies.

if( !is_singular('device') || !is_front_page() ) {
   echo '<div class="container">';
}

Is the logic incorrect?

closed as off-topic by TheDeadMedic, Pieter Goosen, Michael, cybmeta, Robert hue Mar 11 '15 at 5:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    You should use the AND (&&) operator, not the OR (||) operator. OR operators need only one true to execute – Pieter Goosen Mar 9 '15 at 18:08
  • Using && worked, but I am not following logic on that. It is impossible to be on both front-page.php and single-device.php at the same time. So it will always return one true – lime Mar 9 '15 at 18:11
  • The is_front_page() check is somewhat faulty, depending on the context, see this answer for a alternative solution. – Nicolai Mar 9 '15 at 18:15
  • 1
    See this post, I have explained the logic. Helps to break it down, it also confused me in the beginning :-) – Pieter Goosen Mar 9 '15 at 18:19
  • 1
    @PieterGoosen For example when used with pre_get_posts, see here. Faulty is relative I guess... – Nicolai Mar 9 '15 at 18:23
-2

(Expanding on Pieter's comment above, and explaining the logic behind it...)

if() evaluates the conditions in the parentheses, and that evaluation results in either TRUE or FALSE for the sum total (which can change, depending on how the various conditions are combined).

In plain English, if( !is_singular('device') || !is_front_page() ) means, "If it's either true that this is not a single Device post, or true that this is not the front page, then..."

Since a URL on a WordPress site isn't going to both be the front page and a single post (unless you've gone out of your way to configure it as such, I suppose), you probably want to use && instead: if( !is_singular('device') && !is_front_page() ){}

In plain English, the latter if statement means, "If it's both true that this is not a single Device post, and true that this is not the front page, then..."

  • 1
    Way to try and score points with someone else's answer... – vancoder Mar 9 '15 at 23:48
  • I guess it is morally kind of questionable, but at the end, isn't this a collaborative effort to help people. @vancoder – Nicolai Mar 10 '15 at 9:32
  • vancoder... my rep wasn't high enough to comment on the question, and at the time I began writing it out as an answer Peter hadn't yet replied with his link explaining how the logic works out. I was just trying to help explain things. – Travis Seitler Mar 11 '15 at 16:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.