-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?

9
  • 2
    You should use the AND (&&) operator, not the OR (||) operator. OR operators need only one true to execute Commented Mar 9, 2015 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
    Commented Mar 9, 2015 at 18:11
  • The is_front_page() check is somewhat faulty, depending on the context, see this answer for a alternative solution. Commented Mar 9, 2015 at 18:15
  • 1
    See this post, I have explained the logic. Helps to break it down, it also confused me in the beginning :-) Commented Mar 9, 2015 at 18:19
  • 1
    @PieterGoosen For example when used with pre_get_posts, see here. Faulty is relative I guess... Commented Mar 9, 2015 at 18:23

1 Answer 1

-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..."

3
  • 1
    Way to try and score points with someone else's answer...
    – vancoder
    Commented Mar 9, 2015 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 Commented Mar 10, 2015 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. Commented Mar 11, 2015 at 16:20

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